Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Sunday, 31 May 2015

Introduction: 

In this article I will explain how to change or add Meta tags dynamically in asp.net to aspx page in
  c#.net, vb.net. We can add meta tag and page title statically, which is easy. But some time it is needed to add or change Page title and meta tag dynamically i.e. from code behind.
Add Meta tags statically :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Add meta tags Statically to aspx page in asp.net</title>
<meta name="description" content="Welcome to Mazik Solutions: Meta Tag  Example " />
<meta name="keywords" content="ASP.NET, SQL Server " />
</head>
<body>
<form id="form1" runat="server">
<div>
<b> Meta Tags Example</b>
</div>
</form>
</body>
</html>

Add Meta tags dynamically to aspx page :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Add Meta Tags Dynamically</b>
</div>
</form>
</body>
</html>

ASP.NET Code behind Code  using C#:

Add following namespace references:
using System;
using System.Web.UI.HtmlControls;

After add namespaces write the following code in code behind

protected void Page_Load(object sender, EventArgs e)
{
// Add page title
Page.Title = "Add meta tags dynamically to aspx page in asp.net using C#";
//Page description
HtmlMeta pagedesc = new HtmlMeta();
pagedesc.Name = "Description";
pagedesc.Content =
"Mazik Solutions: Add Meta tag Dynamically";
Header.Controls.Add(pagedesc);
//page keywords
HtmlMeta pagekeywords = new HtmlMeta();
pagekeywords.Name = "keywords";
pagekeywords.Content = "ASP.NET, Sql Server";
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 Using VB.NET"
'Page description
Dim pagedesc As New HtmlMeta()
pagedesc.Name = "Description"
pagedesc.Content = "Maziksolution: Add Meta Tag from Code behind"
Header.Controls.Add(pagedesc)
'page keywords
Dim pagekeywords As New HtmlMeta()
pagekeywords.Name = "keywords"
pagekeywords.Content = "ASP.NET , SQL SERVER"
Header.Controls.Add(pagekeywords)
End Sub

End Class

0 comments:

Post a Comment