ASP.NET page title

In ASP.NET when you make a new webpage, by default the page title will be empty. Or in other words, in the resulting HTML-code the title tag within the html tag will be empty. It is good practice to change this and to give your webpage an explicit (page) title. This can easily be done by setting the Title property of the Page object (e.g. in the Page_Load):

protected void Page_Load(object sender, EventArgs e)
    Page.Title = "My title";

The resulting HTML-code looks like this:

My title

You will probably notice something peculiar: the title tag contains the specified text, but preceded and followed by a linefeed. This is sometimes undesirable. For example search engines or other tools analyzing the HTML-code of a webpage may produce incorrect results.

So how can we get rid of those linefeeds?

One way of doing this is by writing your own control adapter for rendering the HTML-code of the title control. Here is how to do this.

Create a folder called ControlAdapters and within this folder create a class called HtmlTitle which inherits from System.Web.UI.Adapters.ControlAdapter. And finally, within the class HtmlTitle, add a method called Render containing the proper code for rendering the title tag:

namespace WebApplication1.ControlAdapters
    public class HtmlTitle : System.Web.UI.Adapters.ControlAdapter
        protected override void Render(HtmlTextWriter writer)

Next, in the folder App_Browsers add a file called Default.browser. And put the following code in this file:

  <browser refID="Default">
      <adapter controlType="System.Web.UI.HtmlControls.HtmlTitle" adapterType="WebApplication1.ControlAdapters.HtmlTitle" />

The main thing is the element “adapter”. It contains the following attributes:
- controlType: Specifies for which control this adapter is used.
- adapterType: Specifies the name, including a full reference to the namespace, of the class containing the code for rendering the HTML-code of the specified control.

After a refresh of your page you will see that the title tag is now rendered without linefeeds:

<title>My title</title>

A final remark. This will only work if the aspx-file contains a (empty) title tag. If you remove the title tag the control adapter will not be used.