ASP.NET 2.0 features significant changes in the model for creating ASP.NET Web pages with code-behind files.
ASP.NET version 1.1 supports two models for writing the code for an ASP.NET Web page: the single-file model and the code-behind model. In the single-file model, the code is inserted into the page in a script block that has the attribute runat="server". The single-file model continues to be supported in version 2.0.
In the code-behind model, the page's markup is in an .aspx page and the programming code is in a separate file. In version 2.0, there have been significant improvements to the code-behind model to make it easier to work with and more robust.
Changes to the Code-Behind Model
In ASP.NET 1.1, a code-behind file for an ASP.NET page defines a class that derives from the Page class. The .aspx page in turn represents another class that derives from the code-behind class. The code-behind class is a complete class definition; it contains instance variables for all controls on the page, explicit event binding using delegates, and so on. ASP.NET also supported a code-behind model that was used primarily in designer-based tools such as Visual Studio 2005.
In the code-behind model for ASP.NET 1.1, the markup for the page is maintained in an .aspx page, and the code is maintained in a separate class file. The link between the .aspx page and its corresponding class file is made the @ Page directive. A typical directive looks like the following:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="SamplePage.aspx.vb" Inherits="SampleProject.SamplePage"%>
<%@ Page Language="c#" AutoEventWireup="false" Codebehind="SamplePage.aspx.cs" Inherits="SampleProject.SamplePage"%>
ASP.NET version 1.1 also supports a variant form of the code-behind model in which the Codebehind attribute of the @ Page directive was replaced with a Src attribute.
Code-Behind Model for ASP.NET version 2.0
The code-behind model for ASP.NET version 2.0 takes advantage of a new language feature known as partial classes. The code-behind file for a page is not a complete class definition. Instead, it includes only the application code you need, such as event handlers. The code-behind partial class does not need to include instance variables or explicit event binding. ASP.NET can infer the control instances and derive event bindings from the markup during compilation.
Note |
|---|
| ASP.NET pages that use Visual Basic as their programming language can use the Handles keyword on methods to explicitly bind the method to an event; doing so can result in slightly faster performance. |
A code-behind file might look like the following:
Partial Class SamplePage
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button1.Click
Label1.Text = "Clicked at " & DateTime.Now.ToString()
End Sub
End Class
using System;
public partial class SamplePage : System.Web.UI.Page
{
protected void Button1_Click(Object sender, EventArgs e)
{
Label1.Text = "Clicked at " + DateTime.Now.ToString();
}
}
The linkage between the .aspx page and the code-behind page is similar to the linkage used with the earlier code-behind model. However, the @ Page directive uses a new CodeFile attribute instead of the Codebehind or Src attribute. In addition, the directive also includes an Inherits attribute to specify a class name for the page, as in the following example:
<%@ Page language="VB" CodeFile="SamplePage.aspx.vb"
Inherits="SamplePage" AutoEventWireup="false" %>
<%@ Page language="C#" CodeFile="SamplePage.aspx.cs"
Inherits="SamplePage" AutoEventWireup="true" %>
The new code-behind model uses a different inheritance model than the code-behind model from previous versions. For details, see ASP.NET Web Page Code Model.
Improvements in the Code-Behind Model
The code-behind model introduced in ASP.NET version 2.0 offers the following improvements over the previous version:
-
The code-behind file is simpler. It includes only the code you write yourself.
-
You can include controls on a page without having to explicitly create instance variables for them in the code-behind class. The code-behind page cannot be out of synch with the controls declared in the markup.
-
Because event binding can be inferred from the declarative controls, you do not need to explicitly bind delegates in the reserved InitializeComponent method.
Note |
|---|
| If you are creating controls and adding them to the page programmatically, you must still explicitly create event bindings. The code-behind model creates control instances and event bindings only for controls that are declared in markup. |
Better Separation of Code and Content
The new code-behind model makes it easier to separate development of the markup and the code. In the older code-behind model, it was not practical to add controls in the markup without having access to the code-behind to add the instance variable at the same time. In the new model, you can create the page layout without needing to have access to the code-behind page.
See Also