如何将多个网格视图绑定到单个事件处理程序

时间:2015-04-28 14:13:56

标签: c# asp.net gridview

我有几个ASP网格视图,当我点击索引值时,我保存了他们的相关ID。我不想为每个人创建多个事件处理程序,而是将它们全部绑定到一个单独的事件处理程序中。 这是我的asp代码:

<asp:GridView ID="grvHighPriority" runat="server" OnSelectedIndexChanged="grv_SelectedIndexChanged" AutoGenerateSelectButton="true"/>

<asp:GridView ID="grvMediumPriority" runat="server" OnSelectedIndexChanged="grv_SelectedIndexChanged" AutoGenerateSelectButton="true"/>

我的C#代码:

protected void grv_SelectedIndexChanged(object sender, EventArgs e)
{
    String groupedErrorID = "";
    GridViewRow row = grvHighPriority.SelectedRow;
    groupedErrorID = row.Cells[1].Text.ToString();
    lblID.Text = groupedErrorID; //for testing purposes
}

目前,eventhandler仅适用于最高优先级网格视图,但我想将所有网格视图绑定到同一个事件处理程序。这可能吗? 提前谢谢!

编辑:

感谢@Enrique找到了一个有效的解决方案:

protected void grv_SelectedIndexChanged(object sender, EventArgs e)
{
    **GridView r = (GridView)sender;**
    String groupedErrorID = "";
    **GridViewRow row = r.SelectedRow;**
    groupedErrorID = row.Cells[1].Text.ToString();
    lblLogin.Text = groupedErrorID; //for testing purposes
}

1 个答案:

答案 0 :(得分:0)

这应该有效

    protected void grv_SelectedIndexChanged(object sender, EventArgs e)
        {
            String groupedErrorID = "";
            GridViewRow row = (((YourControl)sender).Parent.Parent as GridViewRow);        
            groupedErrorID = row.Cells[1].Text.ToString();
            lblID.Text = groupedErrorID; //for testing purposes
        }

YourControl可以像LinkBut​​ton,Button等