// Set keywords and page descriptions
Code
1 Public void setmeta (string key, string des)
2 {
3
4 htmlmeta KEYWORDS = new htmlmeta (), // keywords
5
6 Description = new htmlmeta (); // description
7
8 keywords. Name = "keywords ";
9
10 keywords. content = key;
11
12 description. Name = "Description ";
13
14 description. content = des;
15
16 page. header. Controls. Add (keywords );
17
18 page. header. Controls. Add (description );
19
20}
21
22
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 you need to dynamically modify the header's controls set, if you put it in the page_onAn exception occurs everywhere because the load event is too late. If you want to use a common control to implement this function, we recommend that you package a common class, and then in the masterpager or page page_onLoad.
# 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 ("& gt; br/& gt;", 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