How to add meta tags for a web page dynamically

by Viper 27. February 2009 20:51

If you are using some Content Management System (CMS) for your web site, then there is good chance that all your content along with meta tag content like Title, Description, Keywords etc. is stored in some data source and need to be added to the page dynamically. This all is doable from server side using HtmlMeta html server side control to Controls collection of Header of your page. You just need to add head element to your page and make sure that you have set runat=server for it and you are good to go. See the following snippet how it is done.


protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
 {
  this.Header.Title = "My Page Title";
  HtmlMeta meta = new HtmlMeta();
  meta.Content = "my description for this page";
  meta.Name = "description";
  this.Header.Controls.Add(meta);
 }
}

And on the page I have defined the mark up like this.


<head runat="server">
    <title></title>
</head>

Views: 617

Tags:

.Net | ASP.Net | SEO

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.6.1
Theme by Naveen Kohli

Recent

By Categories