ASP.NET 如何访问父页面上深层嵌套的用户控件
我有一个登录控件,并且 at 在标头控件中嵌套了 2 层 即页面-->标头控制 -->登录控制。我无法使用 FindControl 获取对页面上控件的引用。我希望能够设置控件的可见属性,就像
if (_loginControl != null)
_loginControl.Visible = false;
我最终使用递归 FindControl 方法来查找嵌套控件一样。
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
I have a login control and at is nested 2 deep in a header control
i.e Page --> Header Control --> Login Control. I cannot get a reference to the control on the page using FindControl. I want to be able to set the visible property of the control like
if (_loginControl != null)
_loginControl.Visible = false;
I ended up using a recursive FindControl method to find the nested control.
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需从任何父页面调用此代码,然后通过以下代码获取任何控制
just call this code from you any parent page and then get any control by the following code
登录控件(如果在标记中注册)也将成为代码隐藏页面的实例成员;您可以从代码隐藏类中引用它,就好像它是普通成员一样,使用与您提供的 ID 相同的名称(顺便说一句,我建议对大多数逻辑使用代码隐藏,而不是在标记中内联代码)。
您还可以使用页面的 FindControl() 方法,该方法将在其控件子树中搜索具有给定 ID 的控件。这需要更长的时间,所以我会推荐第一个选项,除非逻辑控制是动态添加的并且您并不总是知道它在那里。
The login control, if it's registered in the markup, will also be an instance member of your codebehind page; you can refer to it from the codebehind class as if it were a normal member, using the same name you provided as the ID (I do recommend using codebehinds for most logic, instead of inlining code in the markup, BTW).
You can also use the FindControl() method of your page, which will search its control subtree for a control with a given ID. That takes longer, so I would recommend the first option unless the logic control is added dynamically and you don't always know it's there.
尝试调用 this.FindControl("_loginControl") 或 this.Page.FindControl("_loginControl")。
方法详情参见MSDN:
http://msdn.microsoft.com/ en-us/library/system.web.ui.control.findcontrol.aspx
Try calling this.FindControl("_loginControl") or this.Page.FindControl("_loginControl").
See MSDN for method details:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol.aspx
一个好的方法是使用:
如果产生 null,则控件不存在。
A good way would be to use:
if that yields null, the control is not there.
您是否需要从用户控件所在的 ASP.NET 页面禁用/隐藏用户控件(或者用户控件是否存在于母版页上)?如果它位于同一页面中,则在 ASP.NET 页面的代码隐藏中执行以下操作:
其中 MyUserControl 是用户控件的 ID。要确定用户控件的 ID,请查看 .aspx 页面的标记,您将看到类似这样的内容:
快乐编程!
Are you needing to disable/hide the User Control from the ASP.NET page it resides on (or does the User Control exist on a master page, say)? If it's in the same page, then in your ASP.NET page's code-behind you'd do:
Where MyUserControl is the ID of your User Control. To determine the ID of your User Control look at the markup of your .aspx page and you will see something like this:
Happy Programming!