Asp. NET: Master Pages and content pages __.net

Source: Internet
Author: User
ASP. NET: Validating Controls ASP. NET: Master Pages and content pages ASP. NET: Skin and style sheets in themes

Load master pages for content pages in a variety of ways

by default, a master page load is assigned to a single content page, and when multiple content pages are loaded onto the same master page, we can bulk load the master page with a configuration file for multiple content pages, and you can dynamically load the master page in the PreInit event.

1, when the master page is loaded for a single content page, the MasterPageFile attribute is automatically added to the page instruction of the content pages
<%@ pagetitle= "" Language= "C #" masterpagefile= "~/masterpage/site.master" ......%>

2. Add a MasterPageFile property to a configuration file when you bulk load a master page for multiple content pages
<configuration>
<system.web>
<pages masterpagefile= "~/masterpage/site.master"/>
</system.web>
</configuration>
At this point, all content pages in the configuration file directory will load the master page in bulk, and normal pages will be ignored by the master page, but remember to remove the MasterPageFile attribute of the page directive in the content page, or you will override the MasterPage attribute in the configuration file.

3. Dynamically load master page in PreInit event
The master page is loaded in the PreInit event and can be dynamically loaded using the following code,
protected void Page_PreInit (Objectsender,eventargs e)
{
MasterPageFile = "~/masterpage/site.master";
}

How to modify the contents of an associated master page in a content page

many times when multiple content pages load the same master page, in order to make the same master page content that is loaded in individual content pages different, we need to modify the content of the master page that is merged in the content page.

1, modify title Tag properties
When the head label for the master page contains the runat= "Server" attribute, you can modify the title Tag property of the content page so that it does not display the default title for the master page.
Method one is the title attribute of the page instruction set by the foreground of the content page.
<%@ page title= "Foreground settings content page title" language= "C #" autoeventwireup= "true" ......%>
Method Two is to set the value of Page.Header.Title through the content page background.
Page.Header.Title = "Programmatic Change content page title";

2, modifying the head label Properties
when the head tag of the master page contains the runat= "Server" attribute, we can modify the tag properties contained in the head tag by using the background code. You can dynamically add the specified label to the head tag, such as the Meta tab, and the Page.header returns the HtmlHead object in the code, representing the head tag, and the Htmlmeta object representing the META tag.
using System.Web.UI.HtmlControls;
.....
.....
protected void Page_Load (Objectsender,eventargs e)
{
   //Modify title Tag properties in the head label
 & nbsp;  Page.Header.Title = "Programmatic Change content page title";
   //Modify other tag attributes contained in the head label, indirectly modifying the page background color
    Style myStyle = new Style ();
 & nbsp;  mystyle.backcolor = System.Drawing.Color.Red;
    Page.Header.StyleSheet.CreateStyleRule (MyStyle, NULL, "HTML");
   //Add new tags meta to head tags
    htmlmeta metakeywords = new Htmlmeta ();
 & nbsp;  metakeywords.name = "KEYWORDS";
    metakeywords.content = "asp.net,c#";
    htmlhead head = Page.header;
    head. Controls.Add (metakeywords);
}

3. Modify other server control properties
A, the Direct amendment method:
Find the master page control by ID ((Label) Master.findcontrol ("Label1")). Text = "Content page";
B. Indirect modification Method:
First in the master page, you wrap the control properties that you want to modify into public properties.
public string Bodytitle
{
Get{return this. Label1.Text;}
set{this. Label1.Text =value;}
}
Then convert the master page to a specific master page type in the content page.
<%@ mastertypevirtualpath= "~/masterpage/masterpage.master"%>
Finally, in the content page, invoke the property that the master page exposes, set its value, Master.bodytitle = "Content page";

third, carefully use the related URL property of the control in the master page

The master page can contain client controls and server-side controls, and when the individual content pages are merged, their associated URL properties need to be resolved to the available URLs for the current content page.

1. When using the client control-related URL properties, you need to resolve the available URLs for the current content page using the Page.resolveurl ("relative path relative to the entire virtual directory") method.
such as need to be modified to "alt=" " ", otherwise not be able to display correctly;"

2, when using the server-side control-related URL properties, automatically resolves to the current content page corresponding to the available URLs,
such as <asp:imageid= "Image1" runat= "Server" imageurl= "~/masterpage/logo.gif"/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.