关于 asp.net:GridView 触发某些事件两次

GridView firing some events twice

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

一些笔记:

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

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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

标记:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<cc1:TabPanel runat="server" HeaderText="View current combinations" ID="TabPanel2" TabIndex="2">
        <ContentTemplate>
           
            <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>
                   
                   
                   
                   
                   
                   
                   

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


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

例如从以下方法签名中删除 Handles grdCombinations.RowEditing

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

'...

End Sub

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