A web page in in VS 2005 is assembled from several source files. Two of them are visible in VS: the markup and the partial class containing custom event handlers and other code you have written. At first sight the main difference with VS 2003 is that the code file looks so much cleaner in 2005. All the declaration and initialization code, generated by VS, is now in a hidden file with the other part of the partial class. At run time the main difference with 2003 is that each page gets compiled into a separate assembly instead of the whole site being compiled into one. As a result your site might be slow in the beginning. To circumvent that you can pre-compile your site. There is a nice article on (pre-) compilation here.
In this post I want to take a deeper look at several pages sharing functionality. There are two scenarios: using master pages or a base page class. Recently I wrestled with the latter in VS 2003, here I will take a deeper look at it to show which of my wishes have been granted.
In 2003 I tried to manipulate the HTML header of my pages. Which is almost impossible to do as the page contain just static HTML
<meta content=”Microsoft Visual Studio .NET 7.1″ name=”GENERATOR”>
<meta content=”C#” name=”CODE_LANGUAGE”>
<meta content=”http://schemas.microsoft.com/intellisense/ie5″ name=”vs_targetSchema”>
To manipulate the header you need something server side. In the feedback to my troubles many a developer suggested to create some server side controls in the header. The only way to do that in VS 2003 is by fiddling with the markup itself
<meta name=”GENERATOR” Content=”Microsoft Visual Studio .NET 7.1″>
<meta name=”CODE_LANGUAGE” Content=”C#”>
<meta name=”vs_targetSchema” content=”http://schemas.microsoft.com/intellisense/ie5″>
<link id=”myStyle” runat=”server” type=”text/css” rel=”stylesheet” />
This does work but completely bypasses the major benefit of a base class as you have to fiddle with the markup in every page.
In VS 2005 this has been more than solved. The header itself is now a server side control.
Now you can manipulate the contents of the header from code:
I’m going to use this to set the stylesheet and the favicon for all my pages in a base class. Add a new class to your web site. VS will advise you to store the class in the special. APP_CODE folder. All code in this folder will be compiled in yet another assembly. This assembly is referenced by all pages in the site.
The header is now a server side control, part of the page control tree. I can add other controls to its control collection.
protected override void OnLoad(EventArgs e)
HtmlLink link1 = new HtmlLink();
link1.Href = “StyleSheet.css”;
link1.Attributes[“text”] = “text/css”;
link1.Attributes[“rel”] = “stylesheet”;
HtmlLink link2 = new HtmlLink();
link2.Href = “MyIcon.ico”;
link2.Attributes[“rel”] = “shortcut icon”;
New in 2.0 is the HtmlLink class which will render the links I need in my pages.
In the code file my pages now inherit from the base class
And this is resulting HTML of my pages. (Breaks and spaces added to improve readability.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml” >
<link href=“StyleSheet.css” text=“text/css” rel=“stylesheet” />
<link href=“MyIcon.ico” rel=“shortcut icon” />
Exactly what I want.
So the final VS 2005 is a big step forward. But there have been many changes over the beta versions of VS 2005. Beta 1 had another organization of code and markup files, as a result it was impossible to use any other base class than System.Web.UI.Page. Thank goodness they changed that. In some of the later CTP’s the Header class had more members. Like the LinkedStyleSheets property. In Dino Esposito’s book Introducing ASP.NET 2.0 there is a nice example using that. Alas this property did not make it in the release. But the good thing about the HtmlLink class is that it’s more generic, it can also be used to link to something like a favicon as well.
But I’m still looking for a way to set the favicon programmatically in 2003</A.