簡體   English   中英

從子GridView中的OnRowDataBound事件訪問父GridView中的ASP.NET控件

[英]Access ASP.NET control in parent GridView in from OnRowDataBound event in child GridView

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

我需要訪問gvChild_RowDataBound事件中分配給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
    }
}

我將如何完成這項任務?

您可以使用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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM