【问题标题】:Finding out which button was clicked in a SelectedIndexChanged event handler of a GridView找出在 GridView 的 SelectedIndexChanged 事件处理程序中单击了哪个按钮
【发布时间】:2012-09-29 02:24:45
【问题描述】:

我们有一个 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

【问题讨论】:

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


    【解决方案1】:

    啊,这个问题有一个简单的解决方案。您的发件人是单击的按钮。试试类似的东西:

    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 ...
        }
    }
    

    【讨论】:

    • 您可以使用任何属性来触发切换,我只是选择了 CommandName,因为它听起来对我来说最直观。
    • 感谢您的快速回复。我明天会试一试,让你知道它是如何工作的。 :-)
    • 没问题,这就是我们等待自己问题的答案时所做的。
    • 不幸的是,我对 vb 不是很熟练,但您真正需要做的就是转换发件人并检查 if 语句中的 CommandName 属性。只需确保在 aspx 文件(或显示逻辑使用的任何文件类型)中设置属性值。
    • 这不应该工作,因为发件人类型是 GridView 类....并注意命令名称,如果它是例如:“选择”,它将选择实际行,我会建议使用 commandArgument 代替,最后我将使用 Button.OnClick 事件而不是 GridView.SelectedIndexChanged,并使用 mGridview.SelectedDataKey("yourKey") 来检索密钥,并使用命令 arg 来了解您单击的女巫按钮跨度>
    【解决方案2】:

    我认为您无法区分 GridView 事件级别的源,因为它是 GridView 在该点引发的事件,它掩盖了较低级别的事件。但是,您可以实现一个行级处理程序来识别哪个按钮用于引发事件并将其设置在某个位置以在 gridview 事件中使用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多