ChimpusDupus 0 Light Poster

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

<%@ Page Language="VB" MasterPageFile="~/lib/Normal.master" 
AutoEventWireup="false" CodeFile="mng_announcement.aspx.vb" 
Inherits="maint_mng_announcement" title="Announcement - Zekiah Technologies Intranet" 
ValidateRequest="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Body" Runat="Server">
      <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0" 
>
        <asp:View ID="NormalView" runat="server">
                    <br />
            <asp:Table ID="tblAnnouncementsOuter" runat="server" 
CellSpacing="0" BorderColor="black" Borderwidth="1" Width="75%" 
cellPadding="2">
                <asp:TableHeaderRow BackColor="gray" ForeColor="white">
                    <asp:TableCell Width="5%" HorizontalAlign="center">
                        &nbsp;
                    </asp:TableCell>
                    <asp:TableCell Width="15%" HorizontalAlign="left">
                        TITLE
                    </asp:TableCell>
                    <asp:TableCell Width="60%" HorizontalAlign="left">
                        ANNOUNCEMENT
                    </asp:TableCell>
                    <asp:TableCell Width="15%" 
HorizontalAlign="center">
                        DATE
                    </asp:TableCell>
                    <asp:TableCell Width="5%" HorizontalAlign="center">
                        ACTIVE
                    </asp:TableCell>
                </asp:TableHeaderRow>
                <asp:TableRow>
                    <asp:TableCell ColumnSpan="5">
                        <asp:Label ID="lblNoAnnouncements" 
runat="server" Text="Sorry, there are no announcements on the database." 
forecolor="red" Visible="false"></asp:Label>
                        <asp:Table ID="tblAnnouncements" runat="server" 
Visible="false" CellSpacing="0" CellPadding="2" Width="100%">
                        </asp:Table>
                    </asp:TableCell>
                </asp:TableRow>
                <asp:TableFooterRow BackColor="gray">
                    <asp:TableCell>
                        <asp:Button ID="btnAdd" runat="server" 
Text="Add" class="btnSubmitType" onmouseover="this.className = 'btnSubmitHov'" 
onmouseout="this.className='btnSubmitType'" CausesValidation="true" />
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:TextBox ID="txtTitle" runat="server" 
Width="90%" ValidationGroup="AddAnnouncement"></asp:TextBox>
                        <asp:RequiredFieldValidator 
ID="RequiredFieldValidator1" runat="server" ErrorMessage="You must submit a title for your 
announcement." 
ControlToValidate="txtTitle">*</asp:RequiredFieldValidator>
                    </asp:TableCell>
                    <asp:TableCell HorizontalAlign="left">
                        <asp:TextBox ID="txtAnnouncement" 
runat="server" TextMode="multiline" Width="90%" 
ValidationGroup="AddAnnouncement"></asp:TextBox>
                        <asp:RequiredFieldValidator 
ID="RequiredFieldValidator2" runat="server" ErrorMessage="You must submit an announcement 
to...well submit an anouncement." 
ControlToValidate="txtAnnouncement">*</asp:RequiredFieldValidator>
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:TextBox ID="txtDate" runat="server" 
Width="90%" ValidationGroup="AddAnnouncement"></asp:TextBox>
                        <asp:RequiredFieldValidator 
ID="RequiredFieldValidator3" runat="server" ErrorMessage="You must submit a date for your 
announcement." 
ControlToValidate="txtDate">*</asp:RequiredFieldValidator>
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:CheckBox ID="chkActive" runat="server" />
                    </asp:TableCell>        
                </asp:TableFooterRow>
            </asp:Table>
            ** Announcement title and text can be written plain text or 
formatted with HTML **
            <asp:ValidationSummary ID="ValidationSummary1" 
runat="server" DisplayMode="BulletList" ShowMessageBox="true" ShowSummary="false" 
ValidationGroup="AddAnnouncement" />
        </asp:View>
        <asp:View ID="EditView" runat="server">
                        <br />
            <asp:Table ID="Table1" runat="server" CellSpacing="0" 
BorderColor="black" Borderwidth="1" Width="75%" cellPadding="2">
                <asp:TableHeaderRow BackColor="gray" ForeColor="white">
                    <asp:TableCell Width="10%" 
HorizontalAlign="center">
                        &nbsp;
                    </asp:TableCell>
                    <asp:TableCell Width="15%" HorizontalAlign="left">
                        TITLE
                    </asp:TableCell>
                    <asp:TableCell Width="60%" HorizontalAlign="left">
                        ANNOUNCEMENT
                    </asp:TableCell>
                    <asp:TableCell Width="10%" 
HorizontalAlign="center">
                        DATE
                    </asp:TableCell>
                    <asp:TableCell Width="5%" HorizontalAlign="center">
                        ACTIVE
                    </asp:TableCell>
                </asp:TableHeaderRow>
                <asp:TableRow ID="rowEdit" BackColor="skyblue">
                    <asp:TableCell HorizontalAlign="center">
                        <asp:Button ID="btnSubmit" runat="server" 
Text="Submit" ValidationGroup="EditAnnouncement" class="btnSubmitType" 
onmouseover="this.className = 'btnSubmitHov'" 
onmouseout="this.className='btnSubmitType'"/>
                        <br />
                        <asp:Button ID="btnCancel" runat="server" 
Text="Cancel" class="btnSubmitType" onmouseover="this.className = 
'btnSubmitHov'" onmouseout="this.className='btnSubmitType'"/>
                    </asp:TableCell>
                    <asp:TableCell HorizontalAlign="left">
                        <asp:TextBox ID="txtTitleEdit" runat="server" 
Width="90%" ValidationGroup="EditAnnouncement"></asp:TextBox>
                        <asp:RequiredFieldValidator 
ID="RequiredFieldValidator6" runat="server" ErrorMessage="You must submit a title for your 
announcement." 
ControlToValidate="txtTitle">*</asp:RequiredFieldValidator>
                    </asp:TableCell>
                    <asp:TableCell HorizontalAlign="left">
                        <asp:TextBox ID="txtAnnouncementEdit" 
runat="server" Width="90%" TextMode="multiline" 
ValidationGroup="EditAnnouncement"></asp:TextBox>
                        <asp:RequiredFieldValidator 
ID="RequiredFieldValidator5" runat="server" ErrorMessage="You must submit an announcement 
to...well submit an anouncement." 
ControlToValidate="txtAnnouncement">*</asp:RequiredFieldValidator>
                    </asp:TableCell>
                    <asp:TableCell HorizontalAlign="center">
                        <asp:TextBox ID="txtDateEdit" runat="server" 
ValidationGroup="EditAnnouncement" Width="90%"></asp:TextBox>
                        <asp:RequiredFieldValidator 
ID="RequiredFieldValidator4" runat="server" ErrorMessage="You must submit a date for your 
announcement." 
ControlToValidate="txtDate">*</asp:RequiredFieldValidator>
                    </asp:TableCell>
                    <asp:TableCell HorizontalAlign="center">
                        <asp:CheckBox ID="chkActiveEdit" runat="server" 
/>
                    </asp:TableCell>
                </asp:TableRow>
                <asp:TableFooterRow BackColor="gray">
                    <asp:TableCell ColumnSpan="5">
                        &nbsp;
                    </asp:TableCell>        
                </asp:TableFooterRow>
            </asp:Table>
            ** Announcement title and text can be written plain text or 
formatted with HTML **
            <asp:ValidationSummary ID="ValidationSummary2" 
runat="server" DisplayMode="BulletList" ShowMessageBox="true" ShowSummary="false" 
ValidationGroup="EditAnnouncement" />
        </asp:View>
    </asp:MultiView>
</asp:Content>

page.aspx.vb

Partial Class maint_mng_announcement
    Inherits System.Web.UI.Page

    Dim AnnouncementID As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles Me.Load
        Call showAnnouncements()
    End Sub

    Sub showAnnouncements()
        Dim Conn As New 
Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
        Dim Cmd As New Data.SqlClient.SqlCommand("SELECT 
announcement_id, announcement_title, announcement_text, announcement_dt, 
announcement_active FROM ANNOUNCEMENT", Conn)
        Dim aReader As Data.SqlClient.SqlDataReader

        Dim TR As TableRow
        Dim TC As TableCell

        Dim imgEdit As ImageButton
        Dim imgTrash As ImageButton
        Dim chkEnabled As CheckBox

        Dim c As Boolean

        Conn.Open()
        aReader = Cmd.ExecuteReader

        tblAnnouncements.Visible = False
        lblNoAnnouncements.Visible = False

        If aReader.Read Then
            tblAnnouncements.Rows.Clear()
            Do
                TR = New TableRow
                TR.BackColor = IIf(c = True, Drawing.Color.White, 
Drawing.Color.LightBlue)

                TC = New TableCell
                TC.HorizontalAlign = HorizontalAlign.Center
                TC.Width = tblAnnouncementsOuter.Rows(0).Cells(0).Width

                imgEdit = New ImageButton
                imgEdit.ImageUrl = "~/images/edit.gif"
                imgEdit.CommandArgument = aReader("announcement_id")
                imgEdit.CausesValidation = False
                imgEdit.ValidationGroup = "Edit"
                AddHandler imgEdit.Command, AddressOf Me.EditItem
                TC.Controls.Add(imgEdit)

                Dim lbl As New Label
                lbl.Text = ("&nbsp")
                TC.Controls.Add(lbl)

                imgTrash = New ImageButton
                imgTrash.ImageUrl = "~/images/t_can.gif"
                imgTrash.CommandArgument = aReader("announcement_id")
                imgTrash.CausesValidation = False
                imgTrash.ValidationGroup = "Trash"
                imgTrash.OnClientClick = "if (confirm('Are you sure you 
want to delete this item?')){form1.submit;}"
                AddHandler imgTrash.Command, AddressOf Me.DeleteItem
                TC.Controls.Add(imgTrash)

                TR.Cells.Add(TC)

                TC = New TableCell
                TC.Width = tblAnnouncementsOuter.Rows(0).Cells(1).Width
                TC.HorizontalAlign = HorizontalAlign.Left
                TC.Text = aReader("announcement_title")
                TR.Cells.Add(TC)

                TC = New TableCell
                TC.Width = tblAnnouncementsOuter.Rows(0).Cells(2).Width
                TC.HorizontalAlign = HorizontalAlign.Left
                TC.Text = aReader("announcement_text")
                TR.Cells.Add(TC)

                TC = New TableCell
                TC.Width = tblAnnouncementsOuter.Rows(0).Cells(3).Width
                TC.HorizontalAlign = HorizontalAlign.Center
                TC.Text = aReader("announcement_dt")
                TR.Cells.Add(TC)

                TC = New TableCell
                TC.Width = tblAnnouncementsOuter.Rows(0).Cells(4).Width
                TC.HorizontalAlign = HorizontalAlign.Center
                chkEnabled = New CheckBox
                chkEnabled.Enabled = False
                chkEnabled.Checked = aReader("announcement_active")
                TC.Controls.Add(chkEnabled)
                TR.Cells.Add(TC)

                tblAnnouncements.Rows.Add(TR)

                If c = True Then
                    c = False
                Else
                    c = True
                End If
            Loop While aReader.Read
            tblAnnouncements.Visible = True
        Else
            lblNoAnnouncements.Visible = True
        End If

        Conn.Close()
    End Sub

    Sub EditItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        AnnouncementID = e.CommandArgument
        MultiView1.ActiveViewIndex += 1
    End Sub

    Sub DeleteItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        AnnouncementID = e.CommandArgument
        Dim Conn As New 
Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
        Dim Cmd As New Data.SqlClient.SqlCommand("DELETE FROM 
ANNOUNCEMENT WHERE announcement_id = " & AnnouncementID, Conn)
        Conn.open()
        Cmd.ExecuteNonQuery()
        Conn.Close()

        Call showAnnouncements()
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles btnAdd.Click
        Dim Conn As New 
Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
        Dim Cmd As New Data.SqlClient.SqlCommand("SELECT ISDATE('" & 
txtDate.Text & "')", Conn)
        Dim DateRead As Data.SqlClient.SqlDataReader
        Conn.open()

        DateRead = Cmd.ExecuteReader
        DateRead.Read()
        If DateRead(0) Then
            'Dim strAnnouncement As String
            'strAnnouncement = txtAnnouncement.Text
            'Encode(strAnnouncement)
            DateRead.Close()
            Cmd.CommandText = "INSERT ANNOUNCEMENT(announcement_title, 
announcement_text, announcement_dt, announcement_active) VALUES('" & 
txtTitle.Text & "', '" & txtAnnouncement.Text & "', '" & txtDate.Text & 
"', " & IIf(chkActive.Checked, 1, 0) & ")"
            Cmd.ExecuteNonQuery()

            txtTitle.Text = ""
            txtAnnouncement.Text = ""
            txtDate.Text = ""
            chkActive.Checked = False

            Call showAnnouncements()
        Else
            DateRead.Close()
        End If
        Conn.Close()
    End Sub

    Sub Encode(ByRef strString As String)
        strString.Replace("<", "&lt;")
        strString.Replace(">", "&gt;")
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles btnSubmit.Click
        Dim Conn As New 
Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
        Dim Cmd As New Data.SqlClient.SqlCommand("SELECT ISDATE('" & 
txtDateEdit.Text & "')", Conn)
        Dim dReader As Data.SqlClient.SqlDataReader
        Conn.Open()
        dReader = Cmd.ExecuteReader
        dReader.Read()
        If dReader(0) = 1 Then
            dReader.Close()

            Cmd.CommandText = "UPDATE ANNOUNCEMENT SET 
announcement_title='" & txtTitleEdit.Text & "', announcement_text='" & 
txtAnnouncementEdit.Text & "', announcement_dt='" & txtDateEdit.Text & "', 
announcement_active=" & IIf(chkActiveEdit.Checked, 1, 0) & " WHERE 
announcement_id=" & btnSubmit.CommandArgument
            Cmd.ExecuteNonQuery()

            txtTitleEdit.Text = ""
            txtAnnouncementEdit.Text = ""
            txtDateEdit.Text = ""
            chkActiveEdit.Checked = False

            MultiView1.ActiveViewIndex -= 1

            Call showAnnouncements()
        Else
            dReader.Close()
        End If

        Conn.Close()
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles btnCancel.Click

        MultiView1.ActiveViewIndex -= 1

        Call showAnnouncements()
    End Sub


    Protected Sub EditView_Activate(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles EditView.Activate
        Dim Conn As New 
Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("sysObjDBConnectionString").ConnectionString)
        Dim Cmd As New Data.SqlClient.SqlCommand("SELECT 
announcement_title, announcement_text, announcement_dt, announcement_active FROM 
ANNOUNCEMENT WHERE announcement_id = " & AnnouncementID, Conn)
        Dim aReader As Data.SqlClient.SqlDataReader
        Conn.Open()
        aReader = Cmd.ExecuteReader
        aReader.Read()

        btnSubmit.CommandArgument = AnnouncementID
        txtTitleEdit.Text = aReader("announcement_title")
        txtAnnouncementEdit.Text = aReader("announcement_text")
        txtDateEdit.Text = aReader("announcement_dt")
        chkActiveEdit.Checked = aReader("announcement_active")
        Conn.Close()
    End Sub

    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles Me.LoadComplete

    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.