Section 6.2. Code-Behind


6.2. Code-Behind

You 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.

Unlike previous versions of Visual Studio, Intellisense now works in content files in VS2005. This is true in code blocks as well as with the HTML and server controls.


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."

The nomenclature "content file," as used in this context, should not be confused with the use of the same term when talking about master pages, where the content file is replaceable content that is inserted onto a master page. Where the meaning is unclear or ambiguous in this book, it will be pointed out. Master pages are covered in Chapter 11.


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

File type

Extension

Page

.aspx

User Control

.ascx

Web Service

.asmx

Master Page

.master


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.

If you rename the content file in Solution Explorer in VS2005, the code-behind file will automatically be renamed identically. However, the class name specified in the Page directive and in the class declaration in the code-behind file will be unchanged. If this is important to you, manually change the class name in those lines of code.


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.

Access Modifiers

The keywords public , protected , private , and internal are access modifiers . An access modifier determines which class methods can see and use a member variable or method. Table 6-3 summarizes the access modifiers.

The default accessibility of members of a class is private . Thus, if there is no access modifier provided for a class member, it will be a private member. Regardless of this circumstance, it is always a good idea to specify the access modifier explicitly to enhance the readability of the code.


Table 6-3. Access modifiers

Access modifier

Restrictions

public

No restrictions. Members marked public are visible to any method of any class.

private

The members in class A that are marked private are accessible only to methods of class A.

protected

The members in class A that are marked protected are accessible to methods of class A and to methods of classes derived from class A.

internal

The members in class A that are marked internal are accessible to methods of any class in A's assembly.

protected internal

The members in class A that are marked protected internal are accessible only to methods of class A, to methods of classes derived from class A, and to any class in A's assembly. This is effectively protected or internal .




Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net