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

时间:2022-10-19 22:28:13

Lets say that I have a header user control in a master page, and want to change a property of the user control depending on what content page is loaded inside of the master page. How might I go about this?

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

Thanks!

4 个解决方案

#1


You can use two methods. The first is by using Page.Master.FindControl('controlID'). Then you can cast it to the type of your user control. The second method is by adding a <%@ MasterType VirtualPath=""> OR <%@ MasterType TypeName=""%> tag to your aspx page. In the VirtualPath add the virtual path to the master page, or the class in the TypeName. You can then access everything with intellisense.

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

#2


first find the user control in the masterpage as below.Then find the control you need to access their property.

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

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;

Hope this helps.

希望这可以帮助。

#3


There's one other method, and that's by making a public property on the master page that exposes the user control.

还有另外一种方法,那就是在母版页上创建一个公开属性,公开用户控件。

#4


Using a public property would work. In the content page's FormLoad method, you could do something like this (VB):

使用公共财产是有效的。在内容页面的FormLoad方法中,您可以执行以下操作(VB):

Dim myMaster as MyMasterPage = CType(Page.Master, MyMasterPage)
myMaster.MyUserControl.Text = "Hello!"

#1


You can use two methods. The first is by using Page.Master.FindControl('controlID'). Then you can cast it to the type of your user control. The second method is by adding a <%@ MasterType VirtualPath=""> OR <%@ MasterType TypeName=""%> tag to your aspx page. In the VirtualPath add the virtual path to the master page, or the class in the TypeName. You can then access everything with intellisense.

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

#2


first find the user control in the masterpage as below.Then find the control you need to access their property.

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

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;

Hope this helps.

希望这可以帮助。

#3


There's one other method, and that's by making a public property on the master page that exposes the user control.

还有另外一种方法,那就是在母版页上创建一个公开属性,公开用户控件。

#4


Using a public property would work. In the content page's FormLoad method, you could do something like this (VB):

使用公共财产是有效的。在内容页面的FormLoad方法中,您可以执行以下操作(VB):

Dim myMaster as MyMasterPage = CType(Page.Master, MyMasterPage)
myMaster.MyUserControl.Text = "Hello!"