Read my other posts on Master pages in ASP.NET
Accessing a master pages properties
So you have a master page, with a lot of content pages using it, and you need the same data, i.e. UserId on all those content pages?
A good way of doing this without breaking the DRY principle is to let content pages access master pages properties.
To do so you need to create a public property in the masters code-behind file like this.
Then you add a @ MasterType directive in all the content pages, where you need to access this property.
This is placed right under @ Page directive. When you have done this, all you need to do to access the MyMasterProperty is to write the following:
Master.MyMasterProperty = “This is a new text.”;
And to read it:
Response.Write(Master.MyMasterProperty)
The same can be done with methods also.
Accessing a master page controls
Say you want to access the controls of a master page, then you can do it in the following way.
We have the following control in the master page.
<asp:Label runat=”server” id=”MasterLabel”></asp:Label>
To programmatically access this control from a content page we need to use the FindControl method in the Master object and cast it to the right type. To do this just use the following code in the code-behind file in the content page.
Label MasterLabel = (Label)Master.FindControl(“MasterLabel”);
MasterLabel.Text = “This text will be displayed in the MasterLabel control in the master page”;
Because we are actually using a string to pass in the control id to the FindControl method, this is not strongly typed, witch I personally prefer (miss typing of the ID wont be found until runtime otherwise). You can expose the control as a property.
Then use the same technic described in the beginning of the post to access the master pages control.