There are two ways to get the content of a master page
1 find the control ID by FindControl
Needed in this event ~ because the content page is loaded before the master page is loaded in the Page_Load
protected void Page_loadcomplete (object sender, EventArgs e)
{
Label2.Text = "Now Time is" + (Master.findcontrol ("Label1") as Label). Text;
if (request.querystring["id"] = = "dy")
{
(Master.findcontrol ("Image1") as Image). IMAGEURL = "~/images/ml0069.jpg";
}
}
2 through strong references
<%@ Page language= "C #" masterpagefile= "~/masterpage.master" autoeventwireup= "true" codefile= "Default2.aspx.cs" inherits= "Default2" title= "Untitled Page"%>
<%@ MasterType virtualpath= "~/masterpage.master"%>
You can then define public properties or methods in the master page
public string GetUserName ()
{
return Page.User.Identity.Name;
}
Calling in the content page
Label1.Text = "Welcome" + master.getusername ();
Use the FindControl method to get a reference to a master page control
Using the master public properties of the content Page Page object, we can implement a reference to the associated master page. It then uses the FindControl method of the master page to implement access to the master page control.
Master Page Masterpage.master:
<%@ Master language= "C #" autoeventwireup= "true" codefile= "MasterPage1.master.cs" inherits= "MasterPage1"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
< HTML xmlns= "http://www.w3.org/1999/xhtml" >
< head runat= "Server" >
< title> master Page </title>
< body>
< form id= "Form1" runat= "Server" >
< Asp:label runat= "server" id= "Masterlabel" > Master page Content </asp:label>
< div>
< Asp:contentplaceholder id= "ContentPlaceHolder1" runat= "Server" >
</asp:contentplaceholder>
</div>
</form>
</body>
Content Page Content1.aspx:
<%@ Page language= "C #" masterpagefile= "~/masterpage1.master" autoeventwireup= "true" codefile= "Content1.aspx.cs" inherits= "Content1" title= "Untitled Page"%>
< script runat= "Server" >
void Page_loadcomplete (Object sender, EventArgs e)
{
Contentlabel. Text = (Master.findcontrol ("Masterlabel") as Label). Text;
}
</script>
< asp:content id= "Content1" contentplaceholderid= "ContentPlaceHolder1" runat= "Server" >
< Asp:label id= "Contentlabel" runat= "Server" > this will display the contents of the master Page Masterlabel control. </asp:label>
</asp:content>
where "Page_loadcomplete" is an event that is triggered when a content page load completes.
Run Result:
Using the MasterType directive to get a reference to a master page control
Compared with the above FindControl method, MasterType appears very direct. By using MasterType, you can create a strongly typed reference to a master page.
Change the Masterpage.master in the FindControl method example as follows:
<%@ Master language= "C #" autoeventwireup= "true" codefile= "MasterPage1.master.cs" inherits= "MasterPage1"%>
< script runat= "Server" >
Public label masterpagelabel//Note: The master Page Label control is strongly typed to facilitate content page access. This method is also used for access to master page properties.
{
get#p# Paging title #e#
{
return Masterlabel;
}
Set
{
Masterlabel = value;
}
}
</script>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
< HTML xmlns= "http://www.w3.org/1999/xhtml" >
< head runat= "Server" >
< title> master Page </title>
< body>
< form id= "Form1" runat= "Server" >
< Asp:label runat= "server" id= "Masterlabel" > Master page Content </asp:label>
< div>
< Asp:contentplaceholder id= "ContentPlaceHolder1" runat= "Server" >
</asp:contentplaceholder>
</div>
</form>
</body>
Change the content1.aspx in the FindControl method example as follows:
<%@ Page language= "C #" masterpagefile= "~/masterpage1.master" autoeventwireup= "true" codefile= "Content1.aspx.cs" inherits= "Content1" title= "Untitled Page"%>
<%@ MasterType virtualpath= "~/masterpage1.master"%>
< script runat= "Server" >
New void Page_Load (Object sender, EventArgs e)
{
Contentlabel. Text = Master.MasterPageLabel.Text;
}
</script>
< asp:content id= "Content1" contentplaceholderid= "ContentPlaceHolder1" runat= "Server" >
< Asp:label id= "Contentlabel" runat= "Server" > this will display the contents of the master Page Masterlabel control. </asp:label>
</asp:content>