Question

I have the following markup snippet on my ASP.NET page

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

I need to access the value assigned to the hfAppID hidden field control in the gvChild_RowDataBound event

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

How would I accomplish this task?

Was it helpful?

Solution

You can use 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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top