Friday, April 24, 2009

Change Page Title and Meta Tags using Code behind.



One of the good things about the asp.net is that we can make everything accessible at runtime.Same can be done with Page title and the Meta Tags.
Simply add Runat="server" to the html tag to make it accessible at run time

like below
For title
<title id="title" runat="server">Initial Title Here</title>

For Meta Tags
<meta name="description" content="description" id="description" runat="server" />
<meta name="keywords" content="keys" id="keywords" runat="server" />

In the code Behind file:
1). Add the namespace
using system.Web.UI.HTMLControls

2). Add the following code in "OnLoad" event

title.InnerText = "Initial Title Here"
keywords.Attributes("content") = "Initial,keywords,here"
description.Attributes("content") = "This is the initial text for description meta tag"

save the change and load the page in browser and simply check the source codes of the web page.