Introduction:
In this article I will explain how to add Meta tags dynamically to aspx page in asp.net.
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.
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. |
|||
|
|||
8 comments :
thanks suresh it really nice .. then what use we i add meta data dynamically ... i'm new for asp.net pls ..
suresh. this is awesome
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.
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 ?
thank you friend
It works.SUPERB.
Thankyou!!!!!
nice one dear suresh. once again you rock.
Its really helpful
www.b2bsalesarrow.com
www.bine24.com
Note: Only a member of this blog may post a comment.