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!
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.
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.
还有另外一种方法,那就是在主页面上创建一个公开属性的公共属性。
Using a public property would work. In the content page's FormLoad method, you could do something like this (VB):
Dim myMaster as MyMasterPage = CType(Page.Master, MyMasterPage)
myMaster.MyUserControl.Text = "Hello!"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.