How to add meta tags for a web page dynamically

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>

comments powered by Disqus

Search

Social

Weather

20.4 °C / 68.6 °F

weather conditions Clouds

Monthly Posts

Blog Tags