6.2. Code-BehindYou can interweave content, such as HTML, text, server controls, and program code in a single file, as was done with traditional ASP. This is known, cleverly, as the single-file model . To see an example of the single-file model, look at CodeSingleFile.aspx , listed in Example 6-1. If you place this file in a folder on your machine, (such as c:\ websites ), create a virtual directory pointing to that folder, named, for example, Websites, and then enter the following URL in your browser: http://localhost/websites/CodeSingleFile.aspx Example 6-1. CodeSingleFile.aspx <%@ Page Language="C#" %> <script runat="server"> protected void btnHello_Click(object sender, EventArgs e) { lblMessage.Text = "Hello. The time is " + DateTime.Now.ToLongTimeString(); } </script> <html> <head runat="server"> <title>Code-Beside</title> </head> <body> <form id="form1" runat="server"> <div> <h1>Code-Beside</h1> <asp:Button ID="btnHello" runat="server" Text="Hello" OnClick="btnHello_Click" /> <br /> <asp:Label ID="lblMessage" runat="server" /> </div> </form> </body> </html> The page will appear, containing a button that will display a text string every time it is clicked.
The single file still requires a Page directive, and any compiled server-code is contained within <script> tags (highlighted in Example 6-1). For this web page, the name of the generated Page class would be CodeSingleFile . The single-file model can produce source control nightmares and difficult-to-maintain pages. ASP.NET addresses these problems by giving developers the ability to separate the executable code from the presentation code. You write the content in a content file, and you write the program logic in the code-behind file (with a .cs or .vb extension, depending on your language choice). The term "code-behind" refers to the "code file behind the form."
There are other types of content files in the ASP.NET Framework besides page files. These are listed in Table 6-2. Table 6-2. Content file types
The code-behind model is the default way that VS2005 operates, automatically creating both a content file and a code-behind file whenever you create a new page (or other item as appropriate). To better understand the code-behind model, look at a simple web site. In VS2005, create a new web site called CodeBehind. Drag a Button and Label control onto the page, along with any other HTML adornment you like. Name the Label control lblMessage and delete the Text property so it initially has nothing to display. Name the Button btnHello , set its Text property to Hello , and give it a default (Click) event handler by going to Design view and double-clicking on the button. The code-behind file will open with the cursor placed for typing in the Click event handler method. Enter the highlighted text listed in Example 6-2. Example 6-2. Code-behind file (Default.aspx.cs) for CodeBehind example using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default: System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnHello_Click(object sender, EventArgs e) { lblMessage.Text = "Hello. The time is " + DateTime.Now.ToLongTimeString(); } } The complete content file is listed in Example 6-3. The two server control declarations are highlighted. Example 6-3. Content file (Default.aspx) for CodeBehind example<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default " %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Code-Behind</title> </head> <body> <form id="form1" runat="server"> <div> <h1>Code-Behind</h1> <asp:Button ID="btnHello" runat="server" Text="Hello" OnClick="btnHello_Click" /> <br /> <asp:Label ID="lblMessage" runat="server" /> </div> </form> </body> </html> The name of the class created from the content file is specified in the Page directive with the Inherits attribute. VS2005 defaults this to the original name of the content file. The name of the code-behind file for this content file is specified in the CodeFile attribute.
In the code-behind file, a partial class is declared that inherits from System.Web.UI.Pag e. public partial class _Default : System.Web.UI.Page Partial classes, new to Version 2.0 of ASP.NET, allow the definition of a class to be split across two or more source files. In the case of code-behind files, this allows VS2005 to hide the details of initializing the controls on the page and allows you to focus on the event handlers and other methods that you created in your .aspx.cs file.
Table 6-3. Access modifiers
|