Dynamically programmatically add title,description and keywords to page in C#.net


In this article I am going to explain about the c# code used for

adding  meta tags like title,  description and keywords to page .
While programming some times we suppose to add meta tags like title description and keywords dynamically. Since meta tags like keywords and description is very important for search engine optimization we supposed to add unique meta tags for each page.

For adding this meta tags we first include the below namespace in the code behind file.
using System.Web.UI.HtmlControls;
After that  in code behind file in page load event set the page title using the below code.
protected void Page_Load(object sender, EventArgse)
    Page.Title = “This is the page title”;
After that to set the meta tags like description and keywords of a page create two instances of  HtmlMeta  Class. One for description and one for keywords. Then set the name property to description and content property to description of the page. Similarly for the second instance set the name property to keywords and content property to keywords of the page .The complete code behind code is given below.
protected void Page_Load(object sender, EventArgse)
    Page.Title = “This is the page title”;
    HtmlMeta descinstance = newHtmlMeta();
    descinstance.Name = “Description”;
    descinstance.Content = “This is page description”;
    HtmlMeta keywordinstance = new HtmlMeta();
    keywordinstance.Name = “Keywords”;
    keywordinstance.Content = “This is page keywords”;
    HtmlHead head = (HtmlHead)Page.Header;
Thus the above code adds the title, description and keywords to the page dynamically.
If needed we can write a database code and bind it dynamically as per your requirement.

Do you like this article. Then comment here or share with your friends. Or like our Facebook page. Or post your comments below

Download Source Code Here

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!