简体   繁体   English

如何从 ASP.NET 用户控件中的父页面访问控件?

[英]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.aspxmasterPage.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 .在上面的代码中, ProductMenuid的的<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.

相关问题 如何从解决方案中的类访问ASP.NET页面上的控件? - How can I access the controls on my ASP.NET page from a class within the solution? 如何从ASP.NET网站(而不是Web应用程序)中的用户控件获取父页面 - How can I get the parent page from a User Control in an ASP.NET Website (not Web Application) 如何从父aspx页面访问子用户控件中的控件 - how to access the controls in child user control from parent aspx page 从其父页面验证ASP.NET用户控件 - Validating an ASP.NET user control from its parent page 从ASP.net中的用户控件访问页面上的文本框 - Access Textbox on Page from User Control in ASP.net 如何从asp.net的父aspx页中找到保留在另一个usercontrol中的用户控件? - How to find user control kept inside another usercontrol from parent aspx page in asp.net? 如何从用户控件访问页面控件? - How to access page controls from user control? 从动态加载的Web用户控件(asp.net C#)中的控件访问值 - Accessing value from the controls within a dynamically loaded web user control (asp.net c#) 将值从HTML控件复制到ASP.NET用户控件 - Copy value from HTML controls to ASP.NET user control 如何从ASP.NET 3.5中的用户控件返回成员数? - How do I return number of members from user controls in ASP.NET 3.5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM