Define Title, Meta Tags, and other Html Headers in the Mastter Page (Master Pages Series 3)

Source: Internet
Author: User

The first two articles briefly explain how to create a Master Page, how to add multiple ContentPlaceHolder to the Master Page, and how to define whether the ContentPlaceHolder is displayed. this section describes the headers of the Master Page. how to define rich header information.

We know that the Header part in HTML can be defined as Search information, Meta information, external JS, and CSS link files. this information may be shared by the entire website. like a CSS file. in this way, you can easily change the layout and style of the entire website through CSS styles.

When you create a Master Page in VS2008, A ContentPlaceHolder is automatically added to the Header, but not in VS2005. however, we can see from the following code that Runat = "server" is added to the Attribute of the Header, so that you can process the Header information in the background code.

<Head runat = "server">
<Title> Master Page </title>
<Link href = "Styles.css" rel = "stylesheet" type = "text/css"/>
</Head>

Step 1: Set the Title information of the Page.

Many of us forget to set the Title of each Page during website design and development. Therefore, no matter which Page the visitor accesses, the Title of the Page is Untitled Page, however, it would be nice for a visitor to obtain the Page information from the Page Title. what we need to do is to set the content-related Title for each Page, so that visitors can see the name and meaning.

ASP. NET can set the Title of a page in the following ways:

1: Put the data directly in <Title> </Ttitle>.

2: Set the Title attribute in <% Page %>.

<% @ Page Language = "C #" MasterPageFile = "~ /Site. master "AutoEventWireup =" true "CodeFile =" About. aspx. cs "Inherits =" About "Title =" About this series "%>

3: Use Page. Title = "Something" or Page. Header. Title = "Infomation" in the background code.

 

Set Title in the background
Protected void Page_Load (object sender, EventArgs e)
{
Page. Title = string. Format ("Master Page series: About ::{ 0: d}", DateTime. Now );
}

Step 2: Automatically Set Title information

In.. NET. all pages inherit from System. web. the Page class of the UID namespace. however, Page Class only defines ASP. NET Page requires few functions, but it also provides many important attributes (such as IsPostBack, IsValid, Request, and Response ). however, many times our web pages require some additional functions or features. A common practice is to create a custom class that inherits the Page class, which includes some custom functions. in this way, you can inherit the custom class when creating the page to get the custom function you want.

In this step, I created a custom class that automatically sets Page Title information based on the Page name.

Create a Base Page Class

Simply add a BasePage. cs (inheriting PageClass) directly, as shown in figure

Since all the Net pages to be created will inherit BasePage. cs, our BasePage Class should inherit from the Class of the Page.

Public class BasePage: System. Web. UI. Page
{
}

Request an ASP.. NET pages must go through a series of steps (Page Life) and generate HTML. we can use the OnEvent method of the Page Class to track data. our Base Page will automatically set the Title information through the LoadComplete method. we only needOverload OnLoadCompleteMethod. By Rewriting, We will automatically set the Page name to the Title of the Page.

Protected override void OnLoadComplete (EventArgs e)
{
// Set the page's title, if necessary
If (string. IsNullOrEmpty (Page. Title) | Page. Title = "Untitled Page ")
{
// Determine the filename for this page
String fileName = System. IO. Path. GetFileNameWithoutExtension (Request. PhysicalPath );

Page. Title = fileName;
}

Base. OnLoadComplete (e );
}

Do not forget to change the base class System. Web. UI. Page of the Page to BasePage, so that we can see the effect, as shown in figure

 

Step 3: set other Header information

We have introduced several methods to set the Title. Here we will introduce how to set other information in the Header, such as <meta> and <Link>.

<Meta>Briefly describe the page information in the following form

<Meta name = "description" content = "word1 MasterPage series"/>

We can also dynamically add this information through the program.

// Programmatically add a <meta> element to the Header
HtmlMeta keywords = new HtmlMeta ();
Keywords. Name = "keywords ";
Keywords. Content = "master page, asp.net, tutorial ";

Page. Header. Controls. Add (keywords );

This article briefly introduces how to present richer Header information.

In the next article, we will give a more in-depth introduction to the Master Page.

 

 

 

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.