Quantcast

Set HTML Header Items Programmatically in ASP.NET 2.0

Get the WebProNews Newsletter:


[ Business]

Among the differences between version 1.1 and 2.0 of .NET Framework is the many new controls.

Even though most of them don’t add new functionality they make a lot of things much easier and cleaner than before. Some of these new controls in ASP.NET are the HTML controls HtmlHead, HtmlMeta and HtmlLink.

They don’t add much new functionality by them selves, but look at how clean the code becomes when you use them:

protected void Page_Load(object sender, EventArgs e)

{

   AddMetaContentType();

   AddMetaTag("keywords", "word1, word2, word3...");

   AddMetaTag("description", "bla bla bla");

  AddStyleSheet("/includes/style.css");

}

private void AddMetaContentType()

{

   HtmlMeta meta = new HtmlMeta();

   meta.HttpEquiv = "content-type";

   meta.Content = Response.ContentType + "; charset=" + Response.ContentEncoding.HeaderName;

   Page.Header.Controls.Add(meta);

}

private void AddMetaTag(string name, string value)

{

   HtmlMeta meta = new HtmlMeta();

   meta.Name = name;

   meta.Content = value;

   Page.Header.Controls.Add(meta);

}

private void AddStyleSheet(string relativePath)

{

   HtmlLink link = new HtmlLink();

   link.Href = relativePath;

   link.Attributes["type"] = "text/css";

   link.Attributes["rel"] = "stylesheet";

   Page.Header.Controls.Add(link);

}

This is much more cleaner than adding literal controls to the page header or whatever trick you used before. Just remember to add a runat=”server” attribute to the <head> tag of your web page.

Comments

Tag:

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

Set HTML Header Items Programmatically in ASP.NET 2.0
About Mads Kristensen
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/ WebProNews Writer
Top Rated White Papers and Resources
  • http://www.mymathgenius.com Shirley Banks

    I have a master/child page setup in ASP.net 2.0

     

    Do i replicate these helper functions for every page? Obviously Not? But then what is the context of the "response" variable.

     

    Could you give an example?

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom