Is it possible to access Masterpage variables from a web user control that is in a child page? I know you can access them in child pages by adding the following
<%@ MasterType VirtualPath="~/Master.master" %>
It doesn’t seem to work when trying to access from a Web control that is inside a child page
User Controls essentially should be unaware of any pages outside the control. The better approach would be to have the control expose properties and events that the page itself (master page or normal) will use to set and retrieve values. Take this simple example:
Then whenever the user control should have a value, the page containing the user control can just handle the RequestingValue event and send the value to the user control. Otherwise just expose a public property of the user control, which you can even make databound, for an even easier solution.
Adding a complete example of the event-driven approach:
WebUserControl1EventArgs.cs
WebUserControl1.ascx
WebUserControl1.ascx.cs
WebForm1.aspx
WebForm1.aspx.cs
WebForm2.aspx
WebForm2.aspx.cs