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 HtmlHeadHtmlMeta 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.

In the year of 2006 it can be hard to avoid doing web development to some extend no matter what kind of developer you are. The lines between the traditional windows applications and the more web based ones have become grayer.

Today, a lot of developers blog about web technologies and it is very easy to spot if the developer is rooted in traditional programming using VB, C++ etc. or from a web root like PHP or ASP.NET.

It’s very obvious when you look at the differences in how they write CSS. I’ve seen examples like the one below hundred of times on the web and even though there’s nothing wrong with it, it makes my eyes twitch every time.

The traditional developer writes CSS like this:

#content{

  border-bottom: 1px solid black;

  border-left: 1px solid black;

  border-right: 1px solid black;

  border-top: 1px solid black;

  font-weight: normal;

  font-size: 11px;

  font-family: Verdana;

}

While the web developer writes the same like this:

>

div#container p#content{

  border: 1px solid black;

  font: normal 11px verdana;

}

Am I exaggerating? Yes I am, but by how much?…

>