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

Asp.Net Dynamically Change (Add) Meta Tags or Page Title & Description in C#, VB.NET

May 20, 2015
Introduction:

Here I will explain how to change or add Meta tags dynamically in
asp.net to aspx page in  c#.netvb.net or dynamically change page title and description in asp.net using c#.net, vb.net or add page title and description dynamically in asp.net using c#.netvb.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

Asp.Net Dynamically Change (Add) Meta Tags or Page Title & Description in C#, VB.NET

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

2 comments :

bhagat said...

Hi Suresh,

i want to add meta tags to page dynamically using jquery please expline me using jquesry web method and how to add that values in function please expline me.

Unknown said...

what about og:image ?

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.