Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
146
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Using Code-behind Files

As we’ve imported that namespace, we have access to all the classes that it contains.

Using Code-behind Files

Most companies that employ web development teams usually split projects into two groups—visual design and functional development—because software engineers are usually poor designers, and designers are often poor engineers. Until now, our ASP.NET pages have contained code render blocks that place VB or C# code directly into the ASP.NET page. The problem with this approach is that there’s no separation between the presentational elements of the page and the application logic. Traditional ASP was infamous for creating “spaghetti” code, which was scattered and intertwined throughout the presentation elements. This made it very tricky to manage the code between development teams, as you’ll know if you’ve ever tried to pick apart someone else’s ASP code. In response to these problems, ASP.NET introduced a new development approach that allows code developers to work separately from the presentation designers who lay out individual pages.

This new method, called code-behind, keeps all of your presentational elements (controls) inside the .aspx file, but moves all of your code to a separate class in a .vb or .cs code-behind file. Consider the following ASP.NET page, which displays a simple button and label:

Visual Basic File: Hello.aspx (excerpt)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Sample Page using VB</title> <script runat="server" language="VB">

Sub Click(s As Object, e As EventArgs) messageLabel.Text = "Hello World"

End Sub </script>

</head>

<body>

<form runat="server">

<asp:Button id="submitButton" Text="Click Me" runat="server" OnClick="Click" />

<asp:Label id="messageLabel" runat="server" /> </form>

87

Chapter 3: VB and C# Programming Basics

</body>

</html>

C#

File: Hello.aspx (excerpt)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Sample Page using C#</title> <script runat="server" language="C#">

void Click(Object s, EventArgs e)

{

messageLabel.Text = "Hello World";

}

</script>

</head>

<body>

<form runat="server">

<asp:Button id="submitButton" Text="Click Me" runat="server" OnClick="Click" />

<asp:Label id="messageLabel" runat="server" /> </form>

</body>

</html>

Let’s see how this example could be separated into the following distinct files:

HelloCodeBehind.aspx

layout, presentation, and static content

HelloCodeBehind.vb or HelloCodeBehind.cs code-behind files containing a custom page class

First, we take all the code and place it in the code-behind file (HelloCodeBehind.vb or HelloCodeBehind.cs). This file is a pure code file, and contains no HTML or other markup tags. Nevertheless, we can still access presentation elements from this file, using their IDs (such as messageLabel) as shown below:

Visual Basic

File: HelloCodeBehind.vb

'First off we import some useful namespaces Imports System

Imports System.Web.UI

Imports System.Web.UI.WebControls

'The partial class

Public Partial Class HelloCodeBehind

88

Using Code-behind Files

Inherits System.Web.UI.Page

'

Here's the Click handler just as it appeared before

Sub Click(s As Object, e As EventArgs)

 

messageLabel.Text = "Hello World"

End Sub

End

Class

 

 

C#

File: HelloCodeBehind.cs

//First off we import some useful namespaces using System;

using System.Web.UI;

using System.Web.UI.WebControls;

//The partial class

public partial class HelloCodeBehind: System.Web.UI.Page

{

// Here's the Click handler just as it appeared before public void Click(Object s, EventArgs e)

{

messageLabel.Text = "Hello World";

}

}

Without code, the main ASP.NET page becomes a bit simpler:

Visual Basic File: HelloCodeBehind.aspx

<%@ Page Language="VB" CodeFile="HelloCodeBehind.vb" Inherits="HelloCodeBehind"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title>Sample Page Code Behind Demo using VB</title> </head>

<body>

<form runat="server">

<asp:Button id="submitButton" Text="Click Me"

 

runat="server" OnClick="Click" />

 

<asp:Label id="messageLabel" runat="server" />

 

</form>

 

</body>

</html>

C#

File: HelloCodeBehind.aspx

<%@ Page Language="C#" CodeFile="HelloCodeBehind.cs" Inherits="HelloCodeBehind"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

89

Chapter 3: VB and C# Programming Basics

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html>

<head>

<title>Sample Page Code Behind Demo using C#</title> </head>

<body>

<form runat="server">

<asp:Button id="submitButton" Text="Click Me" runat="server" OnClick="Click" />

<asp:Label id="messageLabel" runat="server" /> </form>

</body>

</html>

As you can see, the only line that differs between these .aspx pages is the Page directive. Since the .aspx pages now contain only HTML layout, the contents are identical no matter what language you use for the code.

Partial Classes

If you have programmed with ASP.NET 1.1, you may already have noticed the changes in the code-behind model. In ASP.NET 2.0, the code-behind file is cleaner and smaller—a feat it achieves by using a new feature of VB and C# called partial classes. Read on for the details!

The code-behind file is written differently from what you’ve seen so far. While we no longer need <script> tags, we find a class definition in their place. As the VB example shows, we start with three lines that import namespaces for use within the code:

Visual Basic

File: HelloCodeBehind.vb (excerpt)

Imports System

Imports System.Web.UI

Imports System.Web.UI.WebControls

The next lines create a new class, named HelloCodeBehind. Since our code-behind page contains code for an ASP.NET page, our class inherits from the Page class:

Visual Basic

File: HelloCodeBehind.vb (excerpt)

Public Partial Class HelloCodeBehindSample

Inherits System.Web.UI.Page

This is the practical application of inheritance that we mentioned earlier. The HelloCodeBehind class inherits from Page, borrowing all its functionality, and extending it according to the particular needs of the page.

90