Channels ▼


CodeFile and Code-Behind

Code-behind is an ASP.NET feature that keeps ASPX markup (the layout) and related code separated in distinct files. In Visual Studio 2003, code-behind was an imposed and unavoidable feature. Visual Studio 2005 is more flexible and layout/code separation is only the default option.

When you add a new Web Forms and require code and layout separation, a C# (or Visual Basic .NET) class file is created. The class file is named after the .aspx resource simply adding the language extension. If the page is named Default.aspx, the corresponding code-behind class is named Default.aspx.cs. This is just the default name. Although not recommended for the sake of consistency, you should feel free to rename the class file to whatever name you like.'

In Visual Studio 2005, the link between ASPX files and code-behind classes is established through the CodeFile attribute in the @Page directive. A code-behind file is a class that inherits (directly or indirectly) from the System.Web.UI.Page class. The class defines page event handlers and may contain helper methods and local classes.

It is interesting to note that code-behind classes in ASP.NET 2.0 don't include explicitly members that represent Web controls defined in the page. In the previous version, this code was auto-generated by Visual Studio 2003 and inserted in a special region that the page author was heartily recommended not to edit. This region was automatically edited by the IDE. However, due to both bugs on Visual Studio 2003 and special sequences of actions by the page authors, this block of code was often out of sync with the ASPX source code requiring manual editing. Clearly, the model proved quite brittle in the long run.

In ASP.NET 2.0, thanks to the partial class model there's no longer the need to resort to the services of Visual Studio's code generators. You create your code-behind classes without worrying about declaring members to represent Web controls. To exemplify, if your Web page contains a TextBox control named TextBox1, you need a TextBox member named TextBox1 in your code-behind. In ASP.NET 2.0, the existence of this member is assumed in Visual Studio 2005. It is actually declared during the page compilation step and inserted in a dynamically created partial class that completes the code-behind class declaration. The two partial classes are then fused and compiled together as the next step. In the end, the code is generated in the correct form and changed according to any modification in the source. In this way, layout and code-behind classes are always in sync—and by design.

Just because code-behind classes are plain classes, you can use OOP principles to create hierarchies of pages in the context of either a cross-application framework or a single application. In other words, you can create your own classes that derive from Page and use any of these custom classes as the parent of the code-behind. When you do so, you potentially expose yourself to a problem. What if your base class code needs to assume the presence of a Web control in all of derived pages? For example, imagine a scenario where the base class includes a method to set a string in the page's header. To avoid rewriting this code over and over again, you put it in a base class and inherit from there the whole set of pages in the application. The standard compilation model, in this case, entails that a new set of protected members gets created for each derived page. This will break the code in the base class. To avoid problems and instruct the compiler to use members declared in the base class, you set the CodeFileBaseClass attribute in the @Page directive to the name of the base page class. The CodeFileBaseClass attribute was added to ASP.NET 2.0 only in the RTM edition and was not included in any Beta version.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.