[英]Accessing object in parent user control from child user control
所以我有一个用户控件,Parent.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Parent.ascx.cs"
Inherits="Parent" %>
<%@ Register TagPrefix="cc" TagName="Child" Src="~/Child.ascx" %>
<asp:HiddenField ID="hfId" runat="server" />
<cc:Child ID="child1" runat="server" />
我的孩子控件Child.ascx包含一个按钮,在代码隐藏中,我想在该按钮的click事件中访问隐藏字段hfId
的值
我无法使用用户控件属性并在Page_Load
上设置它,因为该隐藏字段的值正在通过Parent.ascx控件中的jQuery事件进行更改
使用以下代码访问子控件的隐藏字段。 this.Parent
将给出父控件并使用FindControl
按ID查找控件。
HiddenField hfID = this.Parent.FindControl("hfId") as HiddenField;
string hiddenvalue = hfID.Value;
如果在页面加载时更改隐藏字段的值,则在按钮单击时,将反映更新的值。
您可以使用以下方式访问孩子的控件:
var hfId = (HiddenField)NamingContainer.FindControl("hfId");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.