
The Code-Behind Page Model
The code-behind page model allows you to keep the markup in one file—the .aspx file—and the programming code in another file. The name of the code file varies according to what programming language you are using.
Note: |
|---|
Not all .NET programming languages allow you to create code-behind files for ASP.NET Web pages. Languages must support partial classes. For example, J# does not support partial classes, and therefore does not support creating code-behind files for ASP.NET pages.
|
For example, if you are working with a page named SamplePage, the markup is in the file SamplePage.aspx and the code is in a file named SamplePage.aspx.vb (for Visual Basic), SamplePage.aspx.cs (for C#), and so on.
Note: |
|---|
The code-behind model used in the .NET Framework version 2.0 is different than the one used in earlier versions.
|
In the code-behind model, the example used in the preceding section for the single-file page would be in two parts. The markup would be in one file (in this example, SamplePage.aspx) and would be similar to the single-file page, as shown in the following code example.
|
<%@ Page Language="VB" CodeFile="SamplePage.aspx.vb"
Inherits="SamplePage" AutoEventWire="false" %>
<html>
<head runat="server" >
<title>Code-Behind Page Model</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1"
runat="server" Text="Label" >
</asp:Label>
<br />
<asp:Button id="Button1"
runat="server"
onclick="Button1_Click"
Text="Button" >
</asp:Button>
</div>
</form>
</body>
</html>
|
|
<%@ Page Language="C#" CodeFile="SamplePage.aspx.cs"
Inherits="SamplePage" AutoEventWireup="true" %>
<html>
<head runat="server" >
<title>Code-Behind Page Model</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1"
runat="server" Text="Label" >
</asp:Label>
<br />
<asp:Button id="Button1"
runat="server"
onclick="Button1_Click"
Text="Button" >
</asp:Button>
</div>
</form>
</body>
</html>
|
There are two differences in the .aspx page between the single-file and the code-behind models. In the code-behind model, there is no script block with the runat="server" attribute. (The page can contain script blocks without the runat="server" attribute if you want to write client-side script in the page.) The second difference is that the @ Page directive in the code-behind model contains attributes that reference an external file (SamplePage.aspx.vb or SamplePage.aspx.cs) and a class. These attributes link the .aspx page to its code.
The code is in a separate file. The following code example shows a code-behind file that contains the same Click event handler as the example for the single-file page.
|
Partial Class SamplePage
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Clicked at " & DateTime.Now.ToString()
End Sub
End Class
|
|
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class SamplePage : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Clicked at " + DateTime.Now.ToString();
}
}
|
The code-behind file contains the complete class declarations in the default namespace. However, the class is declared with the partial keyword, which indicates that the class is not contained entirely in one file. Instead, when the page runs, the compiler reads the .aspx page and the file it references in the @ Page directive, assembles them into a single class, and then compiles them as a unit into a single class.
The partial class file inherits from the page Page class. For more information, see ASP.NET Page Class Overview.