User Id :    Password :      New Member   Forgot Password  
 
How to dynamically add and change meta tags in MasterPage using ASP.NET and CSharp
Description This article demostrates how to dynamically add and change meta tags in masterpage.   No. of Views     4451
  Rating     5
Author Sumit Gupta   Posted On     10 Sep 2010
Tags ASP.NET,C#    

Sample Code   Download Code

Introduction

This article demonstrates how to dynamically add and change meta tags when in MasterPage using ASP.NET and C#.

Now days lots of sites are based on CMS(Content Management System). So there is a need to dynamically add or change meta tags in page for better ranking in search engine.

Using Code

You can use the following simple code for this purpose:

Firstly provide an id to your Head Tag in Master Page


 

Now in the page where you want to change the Meta tags dynamically add the following code sample on the page load event :

	//Find the Head Tag in Master Page
        HtmlHead hd = (HtmlHead)Page.Master.FindControl("myHead");
	//Adding keyword Meta Tag to Head Section
        HtmlMeta htmMeta2 = new HtmlMeta();
        htmMeta2.Attributes.Add("name", "keywords");
        htmMeta2.Attributes.Add("content", "Put your keywords here...");
	//Add Meta Tag to Head 
        hd.Controls.Add(htmMeta2);
	//Adding description Meta Tag to Head Section
        HtmlMeta htmMeta1 = new HtmlMeta();
        htmMeta1.Attributes.Add("name","description");
        htmMeta1.Attributes.Add("content", "Put your description here...");              
	//Add Meta Tag to Head         
	hd.Controls.Add(htmMeta1);        

Now check the view sourse of your page you will find keyword and description meta tag there.

Happy Coding

 

About Author

About Author I am Sumit Gupta working in 3 Pillar Global Pvt. Ltd as Module Lead. I have 7+ year of experience in .Net technologies. I love to explore new technologies and write technical article. Sumit Gupta
No Photo
 
Country India
Company 3 Pillar Global Pvt. Ltd.
Home Page http://www.facebook.com/sumitgupta1225

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code