Dynamically writing Meta tags on Master page from a User Control or any other class file

Raja
Posted by in ASP.NET category on for Intermediate level | Views : 10321 red flag

While developing web sites, you must have came across situations where you need to write Meta tags from the user control or any class files.

At first look it seems very difficult but I think it is not that much.
In My example I am writing Meta contents of my page from a utility class file.

Lets say my following method is in utility.cs file

// method that will write meta tags for keyword and description.
 public static void WriteMetaTags(Control ctrl, string keyword, string desc)

{

Page pge = (Page)ctrl;

HtmlMeta mKey = new HtmlMeta();
mKey.Name = "keyword";
mKey.Content = key;
pge.Header.Controls.Add(mKey);

HtmlMeta mDesc = new HtmlMeta();
mDesc.Name = "description";
mDesc.Content = desc;
pge.Header.Controls.Add(mDesc);

}


I need to call this method from my .aspx page like this.
utility.WriteMetaTags(this, keyword, desc) and its done.

You can also call this method from a user control like
utility.WriteMetaTags(this.parent, keyword, desc)

Enjoy!!!
Page copy protected against web site content infringement by Copyscape

About the Author

Raja
Full Name: Raja Dutta
Member Level:
Member Status: Member
Member Since: 6/2/2008 12:47:48 AM
Country: United States
Regards, Raja, USA
http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)