Tuesday, 27 November 2012

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


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1"runat="server">
<div>
<b>Mukramjah.blogspot.in</b>
</div>
</form>
</body>
</html>

Add the  following name space references
using System;
using System.Web.UI.HtmlControls;
In the page load event add the following 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 = newHtmlMeta();
pagedesc.Name = "Description";
pagedesc.Content =
"Mukramjah.blogspot.in";
Header.Controls.Add(pagedesc);
//page keywords
HtmlMeta pagekeywords = newHtmlMeta();
pagekeywords.Name = "keywords";
pagekeywords.Content = "online material";
Header.Controls.Add(pagekeywords);
}
 
 
Now run the application
If you observe in design metadata tage is added dynamically with attributes.
 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head><metadata name="Description" content="Mukramjah.blogspot.in"/>
<metadata name="keywords" content=""online material"/>
<body>
<form id="form1"runat="server">
<div>
<b>Mukramjah.blogspot.in</b>
</div>
</form>
</body>
</html>

No comments:

Post a Comment