Validation difficulties *sigh*

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 47
Reputation: ChimpusDupus is an unknown quantity at this point 
Solved Threads: 0
ChimpusDupus ChimpusDupus is offline Offline
Light Poster

Validation difficulties *sigh*

 
0
  #1
Jul 14th, 2006
If you haven't guessed by the number of questions I'm asking, I'm in the process of learning ASP.NET as well as using it to update a website.

This problem deals with Validation. Basically, I have two buttons within a table that have the functions of Editing corresponding data in the table and Deleting the corresponding data. When each (image)button is pressed, the appropriate even should occur, per the event handlers added to each control upon creation (showAnnouncements() sub). This works alright except I must click each button twice. On the first click, the page performs a postback and the validation controls for a separate function are activated. Then on the second click, it performs correctly. I have set the validation controls, the validation summary, and the controls that are validated by this separate function to a separate validation group and I have set these imagebuttons' CausesValidation property to false, which should prevent the firing of Validation controls. Why is this occuring, and how can I fix it?

My code is below:

page.aspx
  1. <%@ Page Language="VB" MasterPageFile="~/lib/Normal.master"
  2. AutoEventWireup="false" CodeFile="mng_announcement.aspx.vb"
  3. Inherits="maint_mng_announcement" title="Announcement - Zekiah Technologies Intranet"
  4. ValidateRequest="false" %>
  5. <asp:Content ID="Content1" ContentPlaceHolderID="Body" Runat="Server">
  6. <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"
  7. >
  8. <asp:View ID="NormalView" runat="server">
  9. <br />
  10. <asp:Table ID="tblAnnouncementsOuter" runat="server"
  11. CellSpacing="0" BorderColor="black" Borderwidth="1" Width="75%"
  12. cellPadding="2">
  13. <asp:TableHeaderRow BackColor="gray" ForeColor="white">
  14. <asp:TableCell Width="5%" HorizontalAlign="center">
  15. &nbsp;
  16. </asp:TableCell>
  17. <asp:TableCell Width="15%" HorizontalAlign="left">
  18. TITLE
  19. </asp:TableCell>
  20. <asp:TableCell Width="60%" HorizontalAlign="left">
  21. ANNOUNCEMENT
  22. </asp:TableCell>
  23. <asp:TableCell Width="15%"
  24. HorizontalAlign="center">
  25. DATE
  26. </asp:TableCell>
  27. <asp:TableCell Width="5%" HorizontalAlign="center">
  28. ACTIVE
  29. </asp:TableCell>
  30. </asp:TableHeaderRow>
  31. <asp:TableRow>
  32. <asp:TableCell ColumnSpan="5">
  33. <asp:Label ID="lblNoAnnouncements"
  34. runat="server" Text="Sorry, there are no announcements on the database."
  35. forecolor="red" Visible="false"></asp:Label>
  36. <asp:Table ID="tblAnnouncements" runat="server"
  37. Visible="false" CellSpacing="0" CellPadding="2" Width="100%">
  38. </asp:Table>
  39. </asp:TableCell>
  40. </asp:TableRow>
  41. <asp:TableFooterRow BackColor="gray">
  42. <asp:TableCell>
  43. <asp:Button ID="btnAdd" runat="server"
  44. Text="Add" class="btnSubmitType" onmouseover="this.className = 'btnSubmitHov'"
  45. onmouseout="this.className='btnSubmitType'" CausesValidation="true" />
  46. </asp:TableCell>
  47. <asp:TableCell>
  48. <asp:TextBox ID="txtTitle" runat="server"
  49. Width="90%" ValidationGroup="AddAnnouncement"></asp:TextBox>
  50. <asp:RequiredFieldValidator
  51. ID="RequiredFieldValidator1" runat="server" ErrorMessage="You must submit a title for your
  52. announcement."
  53. ControlToValidate="txtTitle">*</asp:RequiredFieldValidator>
  54. </asp:TableCell>
  55. <asp:TableCell HorizontalAlign="left">
  56. <asp:TextBox ID="txtAnnouncement"
  57. runat="server" TextMode="multiline" Width="90%"
  58. ValidationGroup="AddAnnouncement"></asp:TextBox>
  59. <asp:RequiredFieldValidator
  60. ID="RequiredFieldValidator2" runat="server" ErrorMessage="You must submit an announcement
  61. to...well submit an anouncement."
  62. ControlToValidate="txtAnnouncement">*</asp:RequiredFieldValidator>
  63. </asp:TableCell>
  64. <asp:TableCell>
  65. <asp:TextBox ID="txtDate" runat="server"
  66. Width="90%" ValidationGroup="AddAnnouncement"></asp:TextBox>
  67. <asp:RequiredFieldValidator
  68. ID="RequiredFieldValidator3" runat="server" ErrorMessage="You must submit a date for your
  69. announcement."
  70. ControlToValidate="txtDate">*</asp:RequiredFieldValidator>
  71. </asp:TableCell>
  72. <asp:TableCell>
  73. <asp:CheckBox ID="chkActive" runat="server" />
  74. </asp:TableCell>
  75. </asp:TableFooterRow>
  76. </asp:Table>
  77. ** Announcement title and text can be written plain text or
  78. formatted with HTML **
  79. <asp:ValidationSummary ID="ValidationSummary1"
  80. runat="server" DisplayMode="BulletList" ShowMessageBox="true" ShowSummary="false"
  81. ValidationGroup="AddAnnouncement" />
  82. </asp:View>
  83. <asp:View ID="EditView" runat="server">
  84. <br />
  85. <asp:Table ID="Table1" runat="server" CellSpacing="0"
  86. BorderColor="black" Borderwidth="1" Width="75%" cellPadding="2">
  87. <asp:TableHeaderRow BackColor="gray" ForeColor="white">
  88. <asp:TableCell Width="10%"
  89. HorizontalAlign="center">
  90. &nbsp;
  91. </asp:TableCell>
  92. <asp:TableCell Width="15%" HorizontalAlign="left">
  93. TITLE
  94. </asp:TableCell>
  95. <asp:TableCell Width="60%" HorizontalAlign="left">
  96. ANNOUNCEMENT
  97. </asp:TableCell>
  98. <asp:TableCell Width="10%"
  99. HorizontalAlign="center">
  100. DATE
  101. </asp:TableCell>
  102. <asp:TableCell Width="5%" HorizontalAlign="center">
  103. ACTIVE
  104. </asp:TableCell>
  105. </asp:TableHeaderRow>
  106. <asp:TableRow ID="rowEdit" BackColor="skyblue">
  107. <asp:TableCell HorizontalAlign="center">
  108. <asp:Button ID="btnSubmit" runat="server"
  109. Text="Submit" ValidationGroup="EditAnnouncement" class="btnSubmitType"
  110. onmouseover="this.className = 'btnSubmitHov'"
  111. onmouseout="this.className='btnSubmitType'"/>
  112. <br />
  113. <asp:Button ID="btnCancel" runat="server"
  114. Text="Cancel" class="btnSubmitType" onmouseover="this.className =
  115. 'btnSubmitHov'" onmouseout="this.className='btnSubmitType'"/>
  116. </asp:TableCell>
  117. <asp:TableCell HorizontalAlign="left">
  118. <asp:TextBox ID="txtTitleEdit" runat="server"
  119. Width="90%" ValidationGroup="EditAnnouncement"></asp:TextBox>
  120. <asp:RequiredFieldValidator
  121. ID="RequiredFieldValidator6" runat="server" ErrorMessage="You must submit a title for your
  122. announcement."
  123. ControlToValidate="txtTitle">*</asp:RequiredFieldValidator>
  124. </asp:TableCell>
  125. <asp:TableCell HorizontalAlign="left">
  126. <asp:TextBox ID="txtAnnouncementEdit"
  127. runat="server" Width="90%" TextMode="multiline"
  128. ValidationGroup="EditAnnouncement"></asp:TextBox>
  129. <asp:RequiredFieldValidator
  130. ID="RequiredFieldValidator5" runat="server" ErrorMessage="You must submit an announcement
  131. to...well submit an anouncement."
  132. ControlToValidate="txtAnnouncement">*</asp:RequiredFieldValidator>
  133. </asp:TableCell>
  134. <asp:TableCell HorizontalAlign="center">
  135. <asp:TextBox ID="txtDateEdit" runat="server"
  136. ValidationGroup="EditAnnouncement" Width="90%"></asp:TextBox>
  137. <asp:RequiredFieldValidator
  138. ID="RequiredFieldValidator4" runat="server" ErrorMessage="You must submit a date for your
  139. announcement."
  140. ControlToValidate="txtDate">*</asp:RequiredFieldValidator>
  141. </asp:TableCell>
  142. <asp:TableCell HorizontalAlign="center">
  143. <asp:CheckBox ID="chkActiveEdit" runat="server"
  144. />
  145. </asp:TableCell>
  146. </asp:TableRow>
  147. <asp:TableFooterRow BackColor="gray">
  148. <asp:TableCell ColumnSpan="5">
  149. &nbsp;
  150. </asp:TableCell>
  151. </asp:TableFooterRow>
  152. </asp:Table>
  153. ** Announcement title and text can be written plain text or
  154. formatted with HTML **
  155. <asp:ValidationSummary ID="ValidationSummary2"
  156. runat="server" DisplayMode="BulletList" ShowMessageBox="true" ShowSummary="false"
  157. ValidationGroup="EditAnnouncement" />
  158. </asp:View>
  159. </asp:MultiView>
  160. </asp:Content>

page.aspx.vb
  1.  
  2. Partial Class maint_mng_announcement
  3. Inherits System.Web.UI.Page
  4.  
  5. Dim AnnouncementID As String
  6.  
  7. Protected Sub Page_Load(ByVal sender As Object, ByVal e As
  8. System.EventArgs) Handles Me.Load
  9. Call showAnnouncements()
  10. End Sub
  11.  
  12. Sub showAnnouncements()
  13. Dim Conn As New
  14. Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
  15. Dim Cmd As New Data.SqlClient.SqlCommand("SELECT
  16. announcement_id, announcement_title, announcement_text, announcement_dt,
  17. announcement_active FROM ANNOUNCEMENT", Conn)
  18. Dim aReader As Data.SqlClient.SqlDataReader
  19.  
  20. Dim TR As TableRow
  21. Dim TC As TableCell
  22.  
  23. Dim imgEdit As ImageButton
  24. Dim imgTrash As ImageButton
  25. Dim chkEnabled As CheckBox
  26.  
  27. Dim c As Boolean
  28.  
  29. Conn.Open()
  30. aReader = Cmd.ExecuteReader
  31.  
  32. tblAnnouncements.Visible = False
  33. lblNoAnnouncements.Visible = False
  34.  
  35. If aReader.Read Then
  36. tblAnnouncements.Rows.Clear()
  37. Do
  38. TR = New TableRow
  39. TR.BackColor = IIf(c = True, Drawing.Color.White,
  40. Drawing.Color.LightBlue)
  41.  
  42. TC = New TableCell
  43. TC.HorizontalAlign = HorizontalAlign.Center
  44. TC.Width = tblAnnouncementsOuter.Rows(0).Cells(0).Width
  45.  
  46. imgEdit = New ImageButton
  47. imgEdit.ImageUrl = "~/images/edit.gif"
  48. imgEdit.CommandArgument = aReader("announcement_id")
  49. imgEdit.CausesValidation = False
  50. imgEdit.ValidationGroup = "Edit"
  51. AddHandler imgEdit.Command, AddressOf Me.EditItem
  52. TC.Controls.Add(imgEdit)
  53.  
  54. Dim lbl As New Label
  55. lbl.Text = ("&nbsp")
  56. TC.Controls.Add(lbl)
  57.  
  58. imgTrash = New ImageButton
  59. imgTrash.ImageUrl = "~/images/t_can.gif"
  60. imgTrash.CommandArgument = aReader("announcement_id")
  61. imgTrash.CausesValidation = False
  62. imgTrash.ValidationGroup = "Trash"
  63. imgTrash.OnClientClick = "if (confirm('Are you sure you
  64. want to delete this item?')){form1.submit;}"
  65. AddHandler imgTrash.Command, AddressOf Me.DeleteItem
  66. TC.Controls.Add(imgTrash)
  67.  
  68. TR.Cells.Add(TC)
  69.  
  70. TC = New TableCell
  71. TC.Width = tblAnnouncementsOuter.Rows(0).Cells(1).Width
  72. TC.HorizontalAlign = HorizontalAlign.Left
  73. TC.Text = aReader("announcement_title")
  74. TR.Cells.Add(TC)
  75.  
  76. TC = New TableCell
  77. TC.Width = tblAnnouncementsOuter.Rows(0).Cells(2).Width
  78. TC.HorizontalAlign = HorizontalAlign.Left
  79. TC.Text = aReader("announcement_text")
  80. TR.Cells.Add(TC)
  81.  
  82. TC = New TableCell
  83. TC.Width = tblAnnouncementsOuter.Rows(0).Cells(3).Width
  84. TC.HorizontalAlign = HorizontalAlign.Center
  85. TC.Text = aReader("announcement_dt")
  86. TR.Cells.Add(TC)
  87.  
  88. TC = New TableCell
  89. TC.Width = tblAnnouncementsOuter.Rows(0).Cells(4).Width
  90. TC.HorizontalAlign = HorizontalAlign.Center
  91. chkEnabled = New CheckBox
  92. chkEnabled.Enabled = False
  93. chkEnabled.Checked = aReader("announcement_active")
  94. TC.Controls.Add(chkEnabled)
  95. TR.Cells.Add(TC)
  96.  
  97. tblAnnouncements.Rows.Add(TR)
  98.  
  99. If c = True Then
  100. c = False
  101. Else
  102. c = True
  103. End If
  104. Loop While aReader.Read
  105. tblAnnouncements.Visible = True
  106. Else
  107. lblNoAnnouncements.Visible = True
  108. End If
  109.  
  110. Conn.Close()
  111. End Sub
  112.  
  113. Sub EditItem(ByVal sender As Object, ByVal e As CommandEventArgs)
  114. AnnouncementID = e.CommandArgument
  115. MultiView1.ActiveViewIndex += 1
  116. End Sub
  117.  
  118. Sub DeleteItem(ByVal sender As Object, ByVal e As CommandEventArgs)
  119. AnnouncementID = e.CommandArgument
  120. Dim Conn As New
  121. Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
  122. Dim Cmd As New Data.SqlClient.SqlCommand("DELETE FROM
  123. ANNOUNCEMENT WHERE announcement_id = " & AnnouncementID, Conn)
  124. Conn.open()
  125. Cmd.ExecuteNonQuery()
  126. Conn.Close()
  127.  
  128. Call showAnnouncements()
  129. End Sub
  130.  
  131. Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As
  132. System.EventArgs) Handles btnAdd.Click
  133. Dim Conn As New
  134. Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
  135. Dim Cmd As New Data.SqlClient.SqlCommand("SELECT ISDATE('" &
  136. txtDate.Text & "')", Conn)
  137. Dim DateRead As Data.SqlClient.SqlDataReader
  138. Conn.open()
  139.  
  140. DateRead = Cmd.ExecuteReader
  141. DateRead.Read()
  142. If DateRead(0) Then
  143. 'Dim strAnnouncement As String
  144. 'strAnnouncement = txtAnnouncement.Text
  145. 'Encode(strAnnouncement)
  146. DateRead.Close()
  147. Cmd.CommandText = "INSERT ANNOUNCEMENT(announcement_title,
  148. announcement_text, announcement_dt, announcement_active) VALUES('" &
  149. txtTitle.Text & "', '" & txtAnnouncement.Text & "', '" & txtDate.Text &
  150. "', " & IIf(chkActive.Checked, 1, 0) & ")"
  151. Cmd.ExecuteNonQuery()
  152.  
  153. txtTitle.Text = ""
  154. txtAnnouncement.Text = ""
  155. txtDate.Text = ""
  156. chkActive.Checked = False
  157.  
  158. Call showAnnouncements()
  159. Else
  160. DateRead.Close()
  161. End If
  162. Conn.Close()
  163. End Sub
  164.  
  165. Sub Encode(ByRef strString As String)
  166. strString.Replace("<", "&lt;")
  167. strString.Replace(">", "&gt;")
  168. End Sub
  169.  
  170. Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
  171. System.EventArgs) Handles btnSubmit.Click
  172. Dim Conn As New
  173. Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
  174. Dim Cmd As New Data.SqlClient.SqlCommand("SELECT ISDATE('" &
  175. txtDateEdit.Text & "')", Conn)
  176. Dim dReader As Data.SqlClient.SqlDataReader
  177. Conn.Open()
  178. dReader = Cmd.ExecuteReader
  179. dReader.Read()
  180. If dReader(0) = 1 Then
  181. dReader.Close()
  182.  
  183. Cmd.CommandText = "UPDATE ANNOUNCEMENT SET
  184. announcement_title='" & txtTitleEdit.Text & "', announcement_text='" &
  185. txtAnnouncementEdit.Text & "', announcement_dt='" & txtDateEdit.Text & "',
  186. announcement_active=" & IIf(chkActiveEdit.Checked, 1, 0) & " WHERE
  187. announcement_id=" & btnSubmit.CommandArgument
  188. Cmd.ExecuteNonQuery()
  189.  
  190. txtTitleEdit.Text = ""
  191. txtAnnouncementEdit.Text = ""
  192. txtDateEdit.Text = ""
  193. chkActiveEdit.Checked = False
  194.  
  195. MultiView1.ActiveViewIndex -= 1
  196.  
  197. Call showAnnouncements()
  198. Else
  199. dReader.Close()
  200. End If
  201.  
  202. Conn.Close()
  203. End Sub
  204.  
  205. Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As
  206. System.EventArgs) Handles btnCancel.Click
  207.  
  208. MultiView1.ActiveViewIndex -= 1
  209.  
  210. Call showAnnouncements()
  211. End Sub
  212.  
  213.  
  214. Protected Sub EditView_Activate(ByVal sender As Object, ByVal e As
  215. System.EventArgs) Handles EditView.Activate
  216. Dim Conn As New
  217. Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
  218. Dim Cmd As New Data.SqlClient.SqlCommand("SELECT
  219. announcement_title, announcement_text, announcement_dt, announcement_active FROM
  220. ANNOUNCEMENT WHERE announcement_id = " & AnnouncementID, Conn)
  221. Dim aReader As Data.SqlClient.SqlDataReader
  222. Conn.Open()
  223. aReader = Cmd.ExecuteReader
  224. aReader.Read()
  225.  
  226. btnSubmit.CommandArgument = AnnouncementID
  227. txtTitleEdit.Text = aReader("announcement_title")
  228. txtAnnouncementEdit.Text = aReader("announcement_text")
  229. txtDateEdit.Text = aReader("announcement_dt")
  230. chkActiveEdit.Checked = aReader("announcement_active")
  231. Conn.Close()
  232. End Sub
  233.  
  234. Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As
  235. System.EventArgs) Handles Me.LoadComplete
  236.  
  237. End Sub
  238. End Class
  239.  
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC