There are two ways to get content about a master page
1 find the control ID by FindControl
Required in this event ~ because the content page is loaded first in Page_Load and then the master page is loaded
protected void Page_loadcomplete (object sender, EventArgs e)
{
Label2.Text = "Time is now" + (Master.findcontrol ("Label1")As Label). Text;
if (request.querystring["id"] = = "dy")
{
(Master.findcontrol ("Image1") as Image). IMAGEURL = "~/images/ml0069.jpg";
}
}
2 by strong references
<%@ page language= "C #" masterpagefile= "~/masterpage.master" 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;
}
Called in the content page
Label1.Text = "Welcome" + master.getusername ();
First, use the FindControl method to get a reference to the master page control
Using the master public property of the content Page Page object, we can implement a reference to the associated master page. The master page's FindControl method is then used to implement access to the master page control.
Master Page Masterpage.master:
<%@ Master language= "C #" 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" > Content of Master Page </asp:label>
< div>
< Asp:contentplaceholder id= "ContentPlaceHolder1" runat= "Server" >
</asp:contentplaceholder>
</div>
</form>
</body>
Content Page Content1.aspx:
<%@ page language= "C #" masterpagefile= "~/masterpage1.master" 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" > the contents of the master Page Masterlabel control will be displayed here. </asp:label>
</asp:content>
where "Page_loadcomplete" is an event that is triggered when the content page loads.
Operation Result:
Ii. using the MasterType directive to get a reference to a master page control
Compared to the above FindControl method, MasterType appears very straightforward. 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 #" 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 the master page properties.
{
get#p# Pagination 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" > Content of Master Page </asp:label>
< div>
< asp:contentplaceholder id= "ContentPlaceHolder1" runat= "Server" >
</asp: Contentplaceholder>
</div>
</form>
</body>
Change the content1.aspx in the FindControl method example to the following:
<%@ page language= "C #" masterpagefile= "~/masterpage1.master" Codefile= "Content1.aspx.cs" inherits= "Content1" title= "Untitled page"%>
<%@ MasterType virtualpath= "~/ Masterpage1.master "%>
< script runat=" server ";
new void Page_Load (Object sender, EventArgs e)
&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
contentlabel. Text = Master.MasterPageLabel.Text;
&NBSP;}
</script>
< asp:content ID= " Content1 "contentplaceholderid=" ContentPlaceHolder1 "runat=" Server "
< Asp:label id=" Contentlabel "runat=" Server > The contents of the master Page Masterlabel control are displayed here. </asp:label>
</asp:content>
There are two ways to get the content of a master page--all of it