2014-12-09 5 views
0

В настоящее время, когда я редактирую поле SortOrder в строке gridview, многие события запускаются дважды, и я не уверен, почему это происходит. Это вызывает проблемы, когда я пытаюсь обновить значение поля, так как во время второго пожара данные события теряются.GridView дважды запускает некоторые события

Некоторые примечания:

  • я совершаю Связывание данных при IsPostBack = False в Page_Load
  • FillCombinations() является то, что связывает данные с сеткой
  • Я имею AutoEventWireup="false"
  • Порядок событий стрельбы: RowEditing() x2, когда я нажимаю edit, RowUpdating() x2, когда я нажимаю update и 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 

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

Можете ли вы сообщить нам, что порядок событий уволен при отладке? – Sam

+0

@Sam добавил порядок событий для вас –

+0

Хорошо, какие другие события на уровне страницы уволены? Page_Load, Page_init и т. Д. Дайте нам знать, как именно происходит увольнение. Лучше, если вы добавите точку отладки в начале этих событий, чтобы узнать, какие другие события будут запущены. Не вредите добавлению отладочной точки ко всем событиям в вашем коде. – Sam

ответ

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 кода, так что это вызывает методы дважды выполнить.

+0

отлично! Спасибо за помощь! –

+0

Удовольствие! Повеселись! ура – Sam

Смежные вопросы