如何从内容页面访问母版页中的用户控件?

Bry*_*nny 5 c# asp.net user-controls master-pages

假设我在母版页中有一个标题用户控件,并希望根据母版页内加载的内容页面来更改用户控件的属性.我怎么能这样做?

谢谢!

Sha*_*awn 13

您可以使用两种方法.首先是使用Page.Master.FindControl('controlID').然后,您可以将其强制转换为用户控件的类型.第二种方法是在您的aspx页面中添加<%@ MasterType VirtualPath="">OR <%@ MasterType TypeName=""%>标记.在VirtualPath添加虚拟路径到主页面,或类中TypeName.然后,您可以使用intellisense访问所有内容.


小智 5

首先在母版页中找到用户控件,如下所示.然后找到您需要访问其属性的控件.

UserControl uch = Page.Master.FindControl("ucHeader1") as UserControl;
PlaceHolder phProxylist= ucHeader1.FindControl("phProxy") as PlaceHolder;
DropDownList ddlproxylist1 = ucHeader1.FindControl("ddlProxyList") as DropDownList;
phProxylist.Visible = false;
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.