从子GridView中的OnRowDataBound事件访问父GridView中的ASP.NET控件

时间:2014-04-29 21:03:55

标签: c# asp.net gridview parent-child

我的ASP.NET页面上有以下标记片段

<asp:GridView ID="gvParent" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HiddenField ID="hfAppID" runat="server" />
                <asp:GridView id="gvChild" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvChild_RowDataBound" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

我需要在hfAppID事件中访问分配给gvChild_RowDataBound隐藏字段控件的值

protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        //need to access the hfAppId hidden field control from parent here
    }
}

我将如何完成此任务?

1 个答案:

答案 0 :(得分:3)

您可以使用 Parent.FindControl

protected void gvChild_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var gvChild = sender as GridView;    
        var hfAppID = gvChild.Parent.FindControl("hfAppID") as HiddenField;
        var id = hfAppID.Value;
    }
}