Hii Guys,
Someone asked me yesterday that -
How to dynamically change meta tags when using MasterPage in ASP.NET and C#
I hope lots of beginners are searching the solution all over the net but some articles could be difficult to understand to the beginners so try this one:-
If you have lots of dynamic pages instead of static, and you may want to change the Meta Keywords dynamically which is helpful in SEO, here is the solution :
First of all Provide a id to your Head Tag in Master Page :
<head runat="server" id ="mainHead">
Now in the page where you want to change the Meta tags dynamically add the Following code on the page load event :
//Find the Head Tag in Master Page
HtmlHead hdMain = (HtmlHead)Page.Master.FindControl("mainHead");
HtmlMeta htmMeta = new HtmlMeta();
htmMeta.Attributes.Add("name","description");
htmMeta.Attributes.Add("content", "This is content");
//Add Meta Tag to Head
hdMain.Controls.Add(htmMeta);
//Adding keyword Meta Tag to Head Section
HtmlMeta hm2 = new HtmlMeta();
hm2.Attributes.Add("name", "keywords");
hm2.Attributes.Add("content", "Here you can pass your keywords");
hdMain.Controls.Add(hm2);
And that’s it now view the page source of your page and you will find the description and keyword tags there.
Cool huh?
Happy Programming !!
No comments:
Post a Comment