Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET 2.0 Beta Preview - B. Evjen.pdf
Скачиваний:
26
Добавлен:
24.05.2014
Размер:
15.33 Mб
Скачать

Application and Page Frameworks

New code-behind model

The other option for constructing your ASP.NET 2.0 pages is to build your files using the new codebehind model. I say new because, even though the idea of the code-behind model is the same as it was in previous versions of ASP.NET, the way in which the code-behind model is used in ASP.NET 2.0 is quite a bit different.

To create a new page in your ASP.NET solution that uses the code-behind model, select the page type you want from the Add New Item dialog. Just as many of the pages options have inline options, there also are code-behind file options in this dialog. To build a page using the code-behind model, you have to select the page in the Add New Item dialog and check the Place Code in Separate File check box. The following table shows you the options for pages that use the code-behind model.

File Options Using Code-Behind

Option Creates

 

 

Web Form

.aspx file

 

.aspx.vb or .aspx.cs file

Master Page

.master file

 

.master.vb or .master.cs file

Web User Control

.ascx file

 

.ascx.vb or .ascx.cs file

Web Service

.asmx file

 

.asmx.vb or .asmx.cs file

 

 

The idea of using the code-behind model is to separate the business logic and presentation logic into separate files. Doing this makes it easier to work with your pages, especially if you are working in a team environment where visual designers work on the UI of the page and coders work on the business logic that sits behind the presentation pieces. In the earlier Listings 3-1 and 3-2, you saw how pages using the code-behind model in ASP.NET 1.0/1.1 were constructed. To see the difference in ASP.NET 2.0, take a look at how its code-behind pages are constructed. This is illustrated in Listing 3-4 for the presentation piece and Listing 3-5 for the code-behind piece.

Listing 3-4: An .aspx page that uses the ASP.NET 2.0 code-behind model

VB

<%@ Page Language=”VB” AutoEventWireup=”false” CompileWith=”Default.aspx.vb” ClassName=”Default_aspx” %>

<!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>Simple Page</title> </head>

<body>

(continued)

49

Chapter 3

Listing 3-4: (continued)

<form runat=”server”>

What is your name?<br />

<asp:Textbox ID=”Textbox1” Runat=”server”></asp:Textbox><br /> <asp:Button ID=”Button1” Runat=”server” Text=”Submit” OnClick=”Button1_Click” />

<p><asp:Label ID=”Label1” Runat=”server”></asp:Label></p> </form>

</body>

</html>

C#

<%@ Page Language=”C#” CompileWith=”Default.aspx.cs” ClassName=”Default_aspx” %>

Listing 3-5: A code-behind page

VB

Imports Microsoft.VisualBasic

Partial Class Default_aspx

Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Label1.Text = “Hello “ & TextBox1.Text

End Sub End Class

C#

using System;

using System.Configuration; using System.Web;

using System.Web.Caching; using System.Web.SessionState; using System.Web.Security; using System.Web.Profile; using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;

public partial class Default_aspx

{

void Button1_Click(object sender, EventArgs e)

{

Label1.Text = “Hello “ + Textbox1.Text;

}

}

The .aspx page using this new ASP.NET 2.0 code-behind model has some attributes in the Page directive different from those you are used to. The first is the CompileWith attribute. This is a new attribute in the Page directive and is meant to point to the code-behind page that is used with this presentation

50