Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Dynamically Add Meta tags to Aspx Page in Asp.net using C#, VB.NET

Oct 11, 2012
Introduction:

In this article I will explain how to add Meta tags dynamically to aspx page in
asp.net.

Description:

In Previous posts I explained Differences between appsettings and connection strings, jQuery get hidden field values from asp.net gridview and many articles regarding
Asp.net, Gridview, SQL Server, Ajax, JavaScript etc. Now I will explain how to add Meta tags dynamically to aspx pages in asp.net.

If we want to add Meta tags statically we will set like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Add meta tags dynamically to aspx page in asp.net</title>
<meta name="description" content="Aspdotnet-Suresh offers Asp.net,C#.net,SQL Server,Web Services,Ajax,JavaScript,JQuery,Gridview articles and examples" />
<meta name="keywords" content="asp.net,C#.NET,suresh dasari,GridView,DropDownList,,JavaScript,JQuery,XML" />
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Aspdotnet-Suresh.com Meta Tags Example</b>
</div>
</form>
</body>
</html>
If we want to add Meta tags dynamically to aspx page we need to write the code like this


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Aspdotnet-Suresh.com Meta Tags Example</b>
</div>
</form>
</body>
</html>
After that open code behind page and add the following namespace references


using System;
using System.Web.UI.HtmlControls;
After add namespaces write the following code in code behind

C# code


protected void Page_Load(object sender, EventArgs e)
{
// set page title
Page.Title = "Add meta tags dynamically to aspx page in asp.net";
//Page description
HtmlMeta pagedesc = new HtmlMeta();
pagedesc.Name = "Description";
pagedesc.Content =
"Aspdotnet-Suresh offers Asp.net,C#.net,SQL Server,Web Services,Ajax,JavaScript,JQuery,Gridview articles and examples";
Header.Controls.Add(pagedesc);
//page keywords
HtmlMeta pagekeywords = new HtmlMeta();
pagekeywords.Name = "keywords";
pagekeywords.Content = "asp.net,C#.NET,suresh dasari,GridView,DropDownList,,JavaScript,JQuery,XML";
Header.Controls.Add(pagekeywords);
}
VB.NET Code


Partial Class VBSample
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' set page title
Page.Title = "Add meta tags dynamically to aspx page in asp.net"
'Page description
Dim pagedesc As New HtmlMeta()
pagedesc.Name = "Description"
pagedesc.Content = "Aspdotnet-Suresh offers Asp.net,C#.net,SQL Server,Web Services,Ajax,JavaScript,JQuery,Gridview articles and examples"
Header.Controls.Add(pagedesc)
'page keywords
Dim pagekeywords As New HtmlMeta()
pagekeywords.Name = "keywords"
pagekeywords.Content = "asp.net,C#.NET,suresh dasari,GridView,DropDownList,,JavaScript,JQuery,XML"
Header.Controls.Add(pagekeywords)
End Sub

End Class
If you want to see output run above code snippet and Right click on your page and select view source in that check for title, description and keywords that would be like this

Demo


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

8 comments :

Anbu said...

thanks suresh it really nice .. then what use we i add meta data dynamically ... i'm new for asp.net pls ..

Anonymous said...

suresh. this is awesome

tutors online said...

Thanks for the post. I had been looking for something related and found your web site in the process.. I will definitely be back for more.

Anonymous said...

Hello Suresh

I'm Deepika , Actually I tried this code ,the problem with this is always title only getting displayed but not metadata . I'm unable to understand where I went wrong ,so, if possible can u help me in this context ?

Unknown said...

thank you friend

Anonymous said...

It works.SUPERB.

Thankyou!!!!!

Anonymous said...

nice one dear suresh. once again you rock.

Anonymous said...

Its really helpful


www.b2bsalesarrow.com
www.bine24.com

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.