2012年2月2日 星期四

ASP.NET 如何動態修改 Header 屬性如添加 Meta 標籤 keywords description!


// 設置關鍵字和頁面描述
    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);
    }




















在ASP.NET編輯中,由於經常採用一個頁面通過不同的參數來顯示不同的內容,因此常常需要實現動態輸出不同的html header, 比如title, keywords, descrtptions等。

推薦簡單的做法如下:

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);
}

瀏覽時輸出的頁面源碼便會達到如下效果:

<

由於需要動態修改Header的Controls集合,因此如果放在用户自定義事件的 Page_Onload事件中因为已經太晚而會到處出现異常。對於希望用通用的事件來實現這一功能的場合,建議包裝為一類別,然後在 MasterPager或者Page的Page_OnLoad中調用一下即可。



#region PAGE HEAD

        //Page meta information

        public void BackHeadContent(HtmlControl htmlCtrl)
        {
            Content(htmlCtrl, "網站後台管理系統", "~/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.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();

            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 > 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

TO:http://hi.baidu.com/liyuehui1987/blog/item/d18d8f2f38a4f43c1e3089c0.html

沒有留言:

張貼留言