GridView两次触发一些事件

时间:2014-12-09 02:45:33

标签: asp.net vb.net gridview

目前,当我在gridview行上编辑SortOrder字段时,许多事件将被执行两次,我不确定为什么会发生这种情况。当我在事件数据丢失的第二次火灾期间尝试更新字段的值时,这会导致问题。

一些注意事项:

  • 我在Page_Load
  • IsPostBack = False时正在进行数据绑定
  • FillCombinations()是将数据绑定到网格
  • 的内容
  • 我有AutoEventWireup="false"
  • 触发事件的顺序为:RowEditing() x2点击编辑时,RowUpdating() x2点击更新时CancelRowEditing() x2点击取消时

代码:

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

标记:

<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>

1 个答案:

答案 0 :(得分:3)

您必须删除方法签名中的所有三个handles

E.g。从以下方法签名中删除Handles grdCombinations.RowEditing

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

'...

End Sub

您已经在.aspx代码中指定了句柄,因此这导致方法执行两次。