Simple way to add title, Meta and description tags in child page of masterpage in C#


Monday, November 9, 2009

Create visual studio project and Add two Masterpage.aspx and test.aspx in it.

Open Masterpage.aspx and add this below code in Head tags.

<head id="head1" runat="server">
<title>Master Title</title>
<meta id="Keywords" runat="server" content="" />
<meta id="Description" runat="server" content="" />
<meta name="language" content="english, EN" />
<meta name="revisit-after" content="3 Days" />
<meta name="ROBOTS" content="all" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>


then open test.aspx page and add below code in code behind in page load

protected void Page_Load(object sender, EventArgs e)
{
HtmlHead masterhead = (HtmlHead)Master.FindControl("head1");
masterhead.Title = "test title";

HtmlMeta Keywords = (HtmlMeta)Master.FindControl("Keywords");
Keywords.Name = "keywords";
Keywords.Content = "test keywords,welcome to test keywords";

HtmlMeta description = (HtmlMeta)Master.FindControl("Description");
description.Name = "Description";
description.Content = "This is the test page description";
}


by using above method, you can easily add title, Meta and description tags in child page of masterpage in C#.

Output of test.aspx, you can right click on browser and view the source, you will find all the meta keyword added in this page

test.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="ctl00_head1">
<title>test title </title>
<meta id="ctl00_Keywords" name="keywords" content="test keywords,welcome to test keywords" />
<meta id="ctl00_Description" name="Description" content="This is the test page description" />
<meta name="language" content="english, EN" />
<meta name="revisit-after" content="3 Days" />
<meta name="ROBOTS" content="all" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form name="aspnetForm" method="post" action="test.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEwMDUyNjYzMjgPZBYCZg9kFgICAQ9kFgQCAQ8WBB4EbmFtZQUIa2V5d29yZHMeB2NvbnRlbnQFJnRlc3Qga2V5d29yZHMsd2VsY29tZSB0byB0ZXN0IGtleXdvcmRzZAICDxYEHwAFC0Rlc2NyaXB0aW9uHwEFIVRoaXMgaXMgdGhlIHRlc3QgcGFnZSBkZXNjcmlwdGlvbmRkrw2tXR7g0Lii+IR8lS9SlJoCF/Y=" />
</div>
<div>

</div>
</form>
</body>
</html>



Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)
rajesh@indianic.com

No comments :