您的位置:首页 > 其它

Adding Keyword And Description meta tags to each page by inheritence

2009-02-09 01:14 429 查看
如何加Keyword 和 Description meta tags 到每个页面,看代码:

//Change the line
// public partial class SomePage : System.Web.UI.Page
//to
// public partial class SomePage : PageEx

public partial class SomePage : PageEx
{
protected void Page_Load(object sender, EventArgs e)
{
Title = "Free Piston Power Pack (FP3)";
this.Description = "Your Description of the page";
this.Keywords = new string[] { "keyword1", "keyword1" ,"keyword1"};
}
}

//add the PageEx.cs in the App_Code Directory
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for PageExPage
/// </summary>
public class PageEx : System.Web.UI.Page
{
private HtmlMeta metaDescription = new HtmlMeta();
private HtmlMeta metaKeywords = new HtmlMeta();
public string Description
{
get { return metaDescription.Content; }
set { metaDescription.Content = value; }
}

public string[] Keywords
{
get
{
if (metaKeywords.Content == null)
{
return new string[] { "" };
}
else
{
return metaKeywords.Content.Split(new char[] { ',' });
}
}
set
{
if (value != null)
{
metaKeywords.Content = string.Join(",", value);
}
}
}

public PageEx()
{
Init += new EventHandler(PageEx_Init);
}
void PageEx_Init(object sender, EventArgs e)
{
//Add the description Meta control
metaDescription.Name = "description";
Page.Header.Controls.Add(metaDescription);
//Add the keywords Meta control
metaKeywords.Name = "keywords";
Page.Header.Controls.Add(metaKeywords);
}

}

文章出处:http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx 【直接拉到最后】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐