找出在GridView的SelectedIndexChanged事件处理程序中单击了哪个按钮

时间:2012-09-29 02:24:46

标签: asp.net vb.net gridview event-handling selectedindexchanged

我们有一个GridView,它包含2个“选择”按钮,用于GridView中显示的每一行。

我们想知道是否有办法找出使用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 个答案:

答案 0 :(得分: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 ...
    }
}

答案 1 :(得分:0)

我不认为您可以区分GridView事件级别的源,因为它是GridView在此时引发的事件,它掩盖了较低级别的事件。但是,您可以实现行级处理程序来标识用于引发事件的按钮,并将其设置在某个地方以便在gridview事件中使用。

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

}