Asp.net dynamically sets the title and meta tags of the page

Source: Internet
Author: User

// Set keywords and page descriptions
Public void setmeta (string key, string des)
{
Htmlmeta
KEYWORDS = new htmlmeta (), // keywords
Description = new htmlmeta (); // description
Keywords. Name = "keywords ";
Keywords. content = key;

Description. Name = "Description ";
Description. content = des;

Page. header. Controls. Add (keywords );
Page. header. Controls. Add (description );
}

In ASP. NET programming, because a page is often used to display different content through different parameters, it is often necessary to dynamically output different HTML headers, such as title, keywords, descrtptions, etc.

The recommended method is as follows:

Protected void page_load (Object sender, eventargs E)
{
// Page title
Page. Title = "this is a title and meta test page .";

// Encode/content type
Htmlmeta encode = new htmlmeta ();
Encode. httpequiv = "Content-Type ";
Encode. content = "text/html; charset = UTF-8 ";
Page. header. Controls. Add (encode );

// Language
Htmlmeta lang = new htmlmeta ();
Lang. httpequiv = "content-language ";
Lang. content = "ZH-CN ";
Page. header. Controls. Add (Lang );

// Description
Htmlmeta DESC = new htmlmeta ();
Desc. Name = "Description ";
Desc. content = "test the meta controls ";
Page. header. Controls. Add (DESC );

// Keyword
Htmlmeta KEYWORDS = new htmlmeta ();
Keywords. Name = "keywords ";
Keywords. content = "title, Meta, test, page ";
Page. header. Controls. Add (keywords );

// Link/CSS
Htmllink csslink = new htmllink ();
Csslink. href = "masterpage.css ";
Csslink. Attributes. Add ("rel", "stylesheet ");
Csslink. Attributes. Add ("type", "text/CSS ");
Page. header. Controls. Add (csslink );
}

The page source code output during browsing will achieve the following results:

<Head> <title>
This is a title and meta test page.
</Title> <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8 "/> <meta http-equiv =" content-language "content =" ZH-CN "/> <meta name =" Description "content =" test the meta controls "/> <meta name =" keywords "content =" title, meta, test, page "/> <link href =" masterpage.css "rel =" stylesheet "type =" text/CSS "/>

Because the controls set of the header needs to be modified dynamically, if it is placed in the page_onload event of the custom control, exceptions may occur everywhere because it is too late. If you want to use a common control to implement this function, we recommend that you wrap a common class and then call it in the page_onload of masterpager or page.

# Region page head

// Page meta information

Public void backheadcontent (htmlcontrol htmlctrl)
{
Content (htmlctrl, "website background management system ","~ /Cssstyle/sitestyles.css ");
}

Public void headcontent (htmlcontrol htmlctrl, string strpagetitle)
{
Content (htmlctrl, strpagetitle ,"~ /Cssstyle/styles.css ");
}
Private void content (htmlcontrol htmlctrl, string strtitle, string cssfile)
{
// Title
Htmltitle Title = new htmltitle ();
Title. Text = strtitle;
Htmlctrl. Controls. Add (title );

// Link/cssfile:
Htmllink csslink = new htmllink ();
Csslink. href = cssfile;
Csslink. Attributes. Add ("rel", "stylesheet ");
Csslink. Attributes. Add ("type", "text/CSS ");
Htmlctrl. Controls. Add (csslink );

Htmlmeta
Author = new htmlmeta (), // author
Copyright = new htmlmeta (), // Copyright
Date = new htmlmeta (), // Date
KEYWORDS = new htmlmeta (), // keywords
Description = new htmlmeta (), // description
Robots = new htmlmeta (); // robots

Author. Name = "author ";
Author. content = "insus. Net ";

Copyright. Name = "Copyright ";
Copyright. content = "Copyright 2008 insus. Net ";

Date. Name = "date ";
Date. content = datetime. Now. tow.datestring () + "" + datetime. Now. tow.timestring ();

Keywords. Name = "keywords ";
Keywords. content = "insus ";

Description. Name = "Description ";

Robots. Name = "Robots ";
Robots. content = "all ";

String [] insuswords = "Visual Studio 2003, Visual Studio 2005, Microsoft SQL Server 2005, ASP. NET, ASP. NET 2.0, VB. NET, C #, Ajax, LINQ"
. Replace ("\ r", String. Empty)
. Replace ("\ n", String. Empty)
. Replace ("> Br/>", String. Empty)
. Replace (",", String. Empty)
. Replace ("\ '", String. Empty)
. Split ('');

Foreach (string word in insuswords)
Keywords. content + = word + ",";
If (keywords. content. tostring (). Length & gt; 1024)
{
Keywords. content = keywords. content. substring (0, keywords. content. indexof ("", 1024 ));
}

Description. content = "this Web site use ASP. net2.0 and C # and Ajax technology ";
If (description. content. tostring (). length> 1024)
{
Description. content = description. content. substring (0, description. content. indexof ("", 1024 ));
}

Htmlctrl. Controls. Add (author );
Htmlctrl. Controls. Add (copyright );
Htmlctrl. Controls. Add (date );
Htmlctrl. Controls. Add (keywords );
Htmlctrl. Controls. Add (description );
Htmlctrl. Controls. Add (robots );
}

# Endregion page head

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.