[英]How do I access controls from the parent page from within an ASP.NET User Control?
I have an ASP.NET Web Forms page childPage.aspx
with masterPage.aspx
as the master page.我有一个 ASP.NET Web 窗体页面
childPage.aspx
以masterPage.aspx
作为母版页。 The childPage.aspx
has a user control ( userControl.ascx
) control defined on it. childPage.aspx
上定义了一个用户控件 ( userControl.ascx
) 控件。 Now, I am trying to access the controls on childPage.aspx
from within the user control.现在,我试图从用户控件中访问
childPage.aspx
的控件。 I have tried a handful of different approaches:我尝试了几种不同的方法:
HtmlContainerControl ProductMenu = (HtmlContainerControl)Page.FindControl("ProductMenu");
HtmlContainerControl ProductMenu = (HtmlContainerControl)this.Page.FindControl("ProductMenu");
HtmlContainerControl ProductMenu = (HtmlContainerControl)Parent.FindControl("ProductMenu");
HtmlContainerControl ProductMenu = (HtmlContainerControl)this.Parent.parent.FindControl("ContaintHolder").FindControl("ProductMenu")
In above code, ProductMenu
is the id
of the <div runat="server" />
on childPage.aspx
.在上面的代码中,
ProductMenu
是id
的的<div runat="server" />
上childPage.aspx
。 Now, I am trying to access it from within my user control, but that fails to return the div
.现在,我试图从我的用户控件中访问它,但未能返回
div
。
Please help me out.请帮帮我。 How should I do this?
我该怎么做? Thanks in advance.
提前致谢。
The reason this doesn't work is likely because the FindControl()
method is not recursive .这不起作用的原因可能是因为
FindControl()
方法不是recursive 。 This is called out in the MSDN documentation :这在MSDN 文档中有说明:
This method will find a control only if the control is directly contained by the specified container;
只有在指定容器直接包含控件时,此方法才会查找控件; that is, the method does not search throughout a hierarchy of controls within controls.
也就是说,该方法不会在控件内的控件层次结构中进行搜索。
So, for instance, Page.FindControls()
will only search for controls listed in the Page.Controls
collection;因此,例如,
Page.FindControls()
将只搜索在Page.Controls
集合中列出的Page.Controls
; it won't search the Controls
collection of each of those controls.它不会搜索
Controls
每个这些控件的集合。 As such, Page.FindControl()
would only work if the ProductMenu
were at the top-level of your ASPX page;因此,
Page.FindControl()
仅在ProductMenu
位于 ASPX 页面的顶层时才有效; if it is instead nested within, for instance, a Panel
control then this code won't work.例如,如果它嵌套在
Panel
控件中,则此代码将不起作用。
To resolve this, you'll need to write a recursive function to crawl the control tree.要解决这个问题,您需要编写一个递归函数来爬取控制树。 For instance:
例如:
public Control FindControl(Control parentControl, string controlName) {
foreach (var childControl in parentControl.Controls) {
if (childControl.Id == controlName) return childControl;
var foundControl = FindControl(childControl, controlName);
if (foundControl != null) return childControl;
}
return null;
}
In your case, assuming you'll always be looking for an instance of an HtmlContainerControl
, you could even validate the type and return a strongly typed object, should you choose.在您的情况下,假设您将始终寻找
HtmlContainerControl
的实例,您甚至可以验证类型并返回强类型对象,如果您愿意的话。 That said, if you want to keep it strongly typed while still supporting different types, you could instead use a generic:也就是说,如果你想在支持不同类型的同时保持强类型,你可以改用泛型:
public T FindControl<T>(Control parentControl, string controlName) where T : Control {
foreach (var childControl in parentControl.Controls) {
if (childControl.Id == controlName) return childControl;
var foundControl = FindControl<T>(childControl, controlName);
if (foundControl != null && foundControl is T) return childControl;
}
return null;
}
In addition, if you'll need to do this repeatedly, you might add this as an extension method to the Page
class so it's easily accessible on multiple pages.此外,如果您需要重复执行此操作,您可以将此作为扩展方法添加到
Page
类,以便在多个页面上轻松访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.