The ASP.NET platform has given us controls to create Web
applications in a fraction of the time that it took under the old ASP model.
But one thing that .NET does a poor job of handling right out of the box is the
data included in the <HEAD> tag of a Web page. Often these tags need to
be dynamic with the page, whether to change the character set or encoding of
the page, to add keywords or description tags for search engine crawlers, or to
simply change the title of the page.

You can manipulate the <HEAD> tag either by using
server controls or overriding the Render event of the page. The first method is
useful if your requirements are very specific; the second is a little more
difficult to implement but gives you ultimate control over the page output.

Using server controls

All controls on an ASP.NET page can be created as server
controls, giving you access to change the attributes and properties of the
control on the server. Header controls are no different. The <TITLE>
tag is a good example. By default, the <TITLE> tag is just a static HTML
tag, but you can make it a server control by changing the tag to the code
below:

<title id=”titletag” runat=”server”></title>

In the code-behind page, you will need to add the following declaration
code:

protected System.Web.UI.HtmlControls.HtmlGenericControl titletag;

Now you can access all of the attributes and properties of
this tag using the titletag variable on the server. You can apply this
methodology to any tag you have in the <HEAD> section, or you can apply
it to the <HEAD> tag itself for more flexibility.

Overriding the Render method to control output

The second method is to override the Render method of the
Page object. Remove all HTML except for the <body> tags and what is
inside of them. Inside the Render function, you can build up a string with your
HTML header and send it to the page as it renders by using the HTML text writer
provided in the function definition. Then call the base.Render()
function to render any controls contained on the page. Finally, write out any
footer information for the page, and you have total control of the rendering of
pages.