Matthew Pigram
Matthew Pigram

Reputation: 1430

GridView firing some events twice

Currently when I am editing the SortOrder field on a gridview row many of the events are getting executed twice and I am unsure why this is happening. This is causing problems when I attempt to update the value of a field as during the 2nd fire of the event data is lost.

Some notes:

Code:

Private Sub FillCombinations()

    Dim DT As New DataTable
    DT = DA.GetProductCombinations()

    Me.grdCombinations.DataSource = DT
    Me.grdCombinations.DataBind()

End Sub

Protected Sub grdCombinations_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdCombinations.RowEditing

    grdCombinations.EditIndex = e.NewEditIndex
    FillCombinations()
End Sub

Protected Sub grdCombinations_CancelRowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles grdCombinations.RowCancelingEdit

    grdCombinations.EditIndex = -1
    FillCombinations()
End Sub

Protected Sub grdCombinations_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdCombinations.RowUpdating

    Try
        Dim prodCombID As Integer = Convert.ToInt32(grdCombinations.Rows(e.RowIndex).Cells(0).Text)
        Dim sortVal As Integer = Convert.ToInt32(DirectCast(grdCombinations.Rows(e.RowIndex).Cells(6).Controls(0), TextBox).Text)

        DA.UpdateProductCombination(prodCombID, sortVal)
    Catch ex As Exception
        lblError.Text = "Error Occurred - Could not update Product Combination"
        lblError.Visible = True
        DA.InsertException(-1, "UpdateProductCombination", ex.Message, ex.StackTrace)
    Finally
        'leave edit mode
        grdCombinations.EditIndex = -1
        FillCombinations()
    End Try
End Sub

Markup:

<cc1:TabPanel runat="server" HeaderText="View current combinations" ID="TabPanel2" TabIndex="2">
        <ContentTemplate>
            <asp:Label ID="lblError" ForeColor="Red" runat="server" Visible="false" />
            <asp:GridView ID="grdCombinations" runat="server" AllowSorting="True" DataKeyNames="ProductCombinationID"
                CssClass="ProductCombinationGrid" CellPadding="4" ForeColor="#333333" 
                BackColor="White" BorderWidth="1px" BorderColor="#CC9966" AutoGenerateColumns="False" AutoGenerateEditButton="false"
                OnRowDeleting="grdCombinations_RowDeleting" OnRowEditing="grdCombinations_RowEditing" 
                OnRowCancelingEdit="grdCombinations_CancelRowEditing" OnRowUpdating="grdCombinations_RowUpdating">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <Columns>
                    <asp:BoundField DataField="ProductCombinationID" HeaderText="CombinationID" ReadOnly="true" />
                    <asp:BoundField DataField="ProductID" HeaderText="ProdID" ReadOnly="true" />
                    <asp:BoundField DataField="ProductDescription" HeaderText="Product Description" ReadOnly="true" />
                    <asp:BoundField DataField="SecondaryProductID" HeaderText="Secondary Prod ID" ReadOnly="true" />
                    <asp:BoundField DataField="SecondaryProductDescription" HeaderText="Secondary Product Description" ReadOnly="true" />
                    <asp:BoundField DataField="CombinationType" HeaderText="Combination Type" ReadOnly="true" />
                    <asp:BoundField DataField="SortOrder" HeaderText="Sort Order" />

                    <asp:CommandField ButtonType="Link" EditText="Edit" ShowEditButton="true" DeleteText="Delete" ShowDeleteButton="true" CausesValidation="false" />
                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
        </ContentTemplate>
</cc1:TabPanel>

Upvotes: 1

Views: 1369

Answers (1)

Sam
Sam

Reputation: 2917

You must remove all three handles in your method signatures.

E.g. Remove Handles grdCombinations.RowEditing from the following method signature.

Protected Sub grdCombinations_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdCombinations.RowEditing

'...

End Sub

You are already specifying your handles in your .aspx code so this is causing the methods to execute twice.

Upvotes: 6

Related Questions