In the development process, it is far from enough to simply bind a content page to a fixed master page. It is often necessary to dynamically load the master page. For example, the site is required to provide multiple Available page templates and allow dynamic loading of these templates.
The core of dynamically loading a master page is to set the masterpagefile attribute value. It should be emphasized that this attribute should be set in page_preinit event processing.ProgramBecause the page_preinit event is the first event in the page lifecycle, if you try to set the masterpagefile attribute in the page_load event, a page exception will occur.
Masterpagefile attributes
Used to obtain or set the name of the master page containing the current content.
Syntax:
Public String masterpagefile {Get; set ;}
Property value:The name of the parent master page of the current master page. If the current master page does not have a parent page, it is a blank reference.
Preinit event
Occurs at the beginning of page initialization.
Syntax:
Public event eventhandler preinit
Note:Preinit events are events that can be accessed in the early stages of the page lifecycle. After the preinit event, the personalized information and page topic will be loaded.
The following example shows how to load the master page in a simple and dynamic way. The procedure is as follows:
(1) create a new website and name it 15_02.
(2) Under the solution of the website, right-click the website name and choose "Add new project" from the shortcut menu to open "Add new project ..." In the dialog box, add two master pages named masterpage and othermasterpage respectively, and then add a base master class basemaster to set the page title.
Base master class basemasterSource codeAs follows:
Public class basemaster: masterpage // inherits the masterpage class
{
String _ pagetitle = string. empty;
Public Virtual string titlename // define the attribute titlename through the Virtual keyword
{
Get
{
Return _ pagetitle;
}
}
}
The masterpage and othermasterpage functions on the master page are identical, but the displayed content is different. Therefore, only the masterpage source on the master page is shown below.Code.
<% @ Master language = "C #" inherits = "basemaster" %>
<SCRIPT runat = "server">
Public override string titlename
{
Get
{
Return "loading masterpage. Master ";
}
}
Void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
String selitem = request. querystring ["masterpage"];
Listitem item = dropdownlist1.items. findbyvalue (selitem );
If (item! = NULL)
{
Item. Selected = true;
}
}
}
Void selectedmaster (Object sender, eventargs E)
{
If (dropdownlist1.selectedvalue = "other ")
{
String url = request. Path + "? Masterpage = Other ";
Response. Redirect (URL );
}
}
</SCRIPT>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> </title>
</Head>
<Body leftmargin = "0" topmargin = "0">
<Form ID = "form1" runat = "server">
<Div align = "center">
<Table Height = "100%" border = "0" cellpadding = "0" cellspacing = "0"
Bgcolor = "# ffffff" style = "width: 979px" background = "images/blog homepage. jpg">
<Tr>
<TD align = "right" valign = "TOP" style = "width: 887px; Height: 168px">
</TD>
</Tr>
<Tr>
<TD align = "right" valign = "bottom" style = "height: 53px; width: 887px;">
<Div style = "text-align: Center">
<Asp: dropdownlist id = "dropdownlist1" runat = "server"
Autopostback = "true" validationgroup = "master"
Onselectedindexchanged = "selectedmaster">
<Asp: listitem value = "default"> default master page </ASP: listitem>
<Asp: listitem value = "other"> dynamic master page </ASP: listitem>
</ASP: dropdownlist>
</Div>
</TD>
</Tr>
<Tr>
<TD align = "right" valign = "TOP" style = "width: 887px">
</TD>
</Tr>
<Tr>
<TD valign = "TOP" style = "width: 887px">
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
& Lt; TD width = "244" valign = "TOP" & gt;
</TD>
</Tr>
</Table>
<Asp: contentplaceholder id = "contentplaceholder1" runat = "server">
</ASP: contentplaceholder>
</TD>
</Tr>
<Tr>
<TD style = "width: 887px">
</TD>
</Tr>
<Tr>
<TD align = "center" class = "baseline" style = "width: 887px"> </TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>
The source code of the default content page is as follows: <% @ page Language = "C #" %>
<% @ Mastertype typename = "basemaster" %>
<SCRIPT runat = "server">
Void page_preinit (Object sender, eventargs E)
{
If (request. querystring ["masterpage"] = "other ")
{// Set the masterpagefile attribute of the current page to dynamically load the master page
This. masterpagefile = "othermasterpage. Master ";
}
Else
{
This. masterpagefile = "masterpage. Master ";
}
This. Title = Master. titlename; // you can specify the title of the current page.
}
</SCRIPT>
<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1" runat = "server">
<P> & nbsp; </P>
<H1 align = center>
Dynamically load master pages
</H1>
<P> & nbsp; </P>
</ASP: content>