CodeFile and Code-Behind

The VS2003 implementation of code-behind was mandatory but also brittle. VS 2005 is more flexible, and layout/code separation is optional.


May 10, 2006
URL:http://www.drdobbs.com/windows/codefile-and-code-behind/187202007

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.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.