0

我們有一個GridView,其中包含GridView中顯示的每一行的兩個「選擇」按鈕。找出在GridView的SelectedIndexChanged事件處理程序中單擊了哪個按鈕

我們想知道是否有方法通過使用SelectedIndexChanged處理程序來找出哪個按鈕被點擊。

這種編碼顯示我們的按鈕:

<asp:UpdatePanel 
    ID="UpdatePanelParentsSummary" 
    runat="server" 
    UpdateMode="Conditional"> 

    <ContentTemplate> 
     <p>Parent Search: 
      <asp:TextBox 
       ID="TextBoxSearch" 
       runat="server" 
       Width="207px" 
       Text="ALL"> </asp:TextBox> 

      <asp:Button 
       ID="ButtonSearch" 
       runat="server" 
       Text="Search" /> 

      <asp:Button 
       ID="ButtonSearchAll" 
       runat="server" 
       Text="Show ALL Parents" /> 

      <br /> 
     </p> 

     <asp:GridView 
      ID="GridViewParentsSummary" 
      runat="server" 
      AllowPaging="True" 
      AllowSorting="True" 
      AutoGenerateColumns="False" 
      DataKeyNames="ID" 
      > 

      <Columns> 
       <asp:BoundField 
        DataField="ID" 
        HeaderText="ID" 
        SortExpression="ID" InsertVisible="False" ReadOnly="True" Visible="False" /> 

       <asp:BoundField 
        DataField="FatherName" 
        HeaderText="FatherName" 
        SortExpression="FatherName" /> 

       <asp:BoundField DataField="MotherName" HeaderText="MotherName" 
        SortExpression="MotherName" /> 

       <asp:ButtonField 
        ButtonType="Button" 
        CommandName="Select" 
        Text="Select Details" /> 

       <asp:ButtonField 
        ButtonType="Button" 
        CommandName="Select" 
        Text="New Person To Release Child" /> 
      </Columns> 
     </asp:GridView> 
    </ContentTemplate> 
</asp:UpdatePanel> 

這是的SelectedIndexChanged處理程序的代碼:

Protected Sub GridViewParentsSummary_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridViewParentsSummary.SelectedIndexChanged 

    IntParentsID = GridViewParentsSummary.DataKeys(GridViewParentsSummary.SelectedIndex).Value 

    Response.Redirect("AuthorizationForChildReleaseDetails.aspx") 
End Sub 

回答

2

啊,有一個簡單的解決這個問題。您的發件人是點擊的按鈕。剛剛嘗試類似:

ButtonField buttonClicked = sender as ButtonField; 
if (buttonClicked != null) { 
    String commandName = buttonClicked.CommandName; 

    if (commandName.equals("Command1") { 
     ... do something awesome ... 
    } else if (commandName.equals("Command2")) { 
     ... do something less awesome ... 
    } 
} 
+0

您可以使用任何屬性來觸發開關,我只是選擇CommandName,因爲它聽起來對我來說是最直觀的。 –

+0

感謝您的快速回復。我明天會試試看,並告訴你它是如何工作的。 :-) –

+0

沒問題,這是我們在等待我們自己的問題的答案時所做的。 –

0

我不認爲你可以在GridView的事件級別源之間進行區分,因爲它是在這一點上它掩蓋了較低級別的情況下,被GridView控件引發的事件。但是,您可以實現行級處理程序,以確定哪個按鈕用於引發事件並將其設置在某處以便在gridview事件中使用。

protected void GridView_RowCommand(object sender, CommandEventArgs e) 
{ 
    e.CommandArgument ....contains the argument name 
    .... 

} 
相關問題