【发布时间】:2014-12-09 02:45:32
【问题描述】:
目前,当我在 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>
【问题讨论】:
-
您能告诉我们调试时触发事件的顺序吗?
-
@Sam 为您添加了事件顺序
-
好的,还有哪些页面级事件被触发? Page_Load、Page_init 等。让我们确切地知道事件是如何被触发的。最好在这些事件的开头添加一个调试点以查看触发了哪些其他事件。为后面代码中的所有事件添加调试点没有害处。
-
还有,您的 FillCombinations() 方法中有什么?
-
@Sam 添加了
FillCombinations()代码,Page_Load 是唯一的其他事件,它调用FillCombinations()但仅在IsPostBack = False时调用