Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

434 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

Exploring ASP.NET Web Applications

ASP.NET applications include Web pages that are used to interact with a user. These pages are referred to as Web forms. When you browse an ASP.NET Web site, you use its Web forms to retrieve and update information. You can retrieve and update information on an ASP.NET Web site by using Web form server controls.These controls help design the interface of your application and process user data at the ser ver.

In this section, I will explain the concept of Web forms and describe the controls that are provided by ASP.NET.

Introducing Web Forms

Web forms are ASP.NET components that enable you to create interactive and dynamic Web pages. Web forms also provide you with a rich set of controls that can programmed in any .NET-compatible language, such as Visual Basic .NET and Visual C#.

A Web form comprises two components, programming logic and form interface. By separating the two, ASP.NET makes it easy for you to concentrate on the programming logic of the application without worrying about how the text will be rendered on the Web form. The two components of a Web form are explained as follows:

Visual component. The visual component of a Web form is the .aspx file that contains the code for rendering a Web form.

Programming logic. The programming logic of a Web form is the logic used to generate the output for a Web form. The default option provided by Visual Studio .NET is to create this file as a code-behind file. When you create a code-behind file, the extension of the file is .aspx.cs or

.aspx.vb, depending upon whether you are coding your application in Visual C# or Visual Basic .NET. However, you can also create this code in a code-inline model in which the code is written in the .aspx file. This method was used in ASP 3.0.

To create high performance Web forms, you should understand how Web forms are processed. There are two processing methods, client-side and server-side. I will discuss these methods in detail in this section.

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

435

 

 

 

 

Server-Side Processing

In the server-side processing method, the request from the client is passed to the server for validation. For example, when a user needs to add details about a new flight, the user specifies the required information on a Web form and submits the information to the server for processing. This is an example of server-side processing. In the SkyShark Airlines application, most of the processing needs to be performed at the server side. Therefore, you will see that this method is used most often in the Web forms created in the subsequent chapters of this project.

Each time a Web form is posted to the server for processing, data is processed and the same form or another form is displayed to the user. For example, when you specify flight details and query the database for status of a flight, the information is retrieved and the Web form is reconstructed to display the status of the flight.

Before a Web form is displayed, the Page_Load event for the Web form is generated. All server controls are loaded for the Web form in the Page_Load event. Similarly, when the user navigates away or closes a Web form, the Page_Unload event is generated. In the Page_Unload event, the page is removed from the memory and any clean-up code that might be required to free resources is executed.

This procedure has inherent performance overheads. Consider the case where you specify a ticket number and submit the form to retrieve ticket details. All controls will be reinstantiated each time the page is loaded. However, you need to change the values in one or more controls. There should be a way to retain the control state between round trips on the server. ASP.NET offers a solution to this problem.By setting the IsPostBack property of controls to true, you can retain the state of a control between round trips. In addition, the initialization code executes only after the IsPostBack property is set to true, which helps in averting performance overheads.

TIP

Round trips are generated when a user requests for the Web form that is displayed.

Client-Side Processing

The client-side processing method is used to perform client-side validation. For example, if a client needs to print a report, it needs to process the document at the

436 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

client end and not at the server end. If you use client-side validation code, the load on your server can be considerably reduced. The performance of your application improves. In the SkyShark Airlines application, I will use client-side processing to print the reservation ticket that is generated for a passenger.

Web Form Server Controls

Web form server controls are used for designing the user interface of the application and posting data to the server. Although similar to HTML controls in appearance and operation, server controls run on the server. It is easier to program with these controls because the methods, properties, and events exposed by these controls are consistent and utilize the .NET Framework class library. In this section, I will examine the server controls that are provided by ASP.NET.

Summary of Web Form Server Controls

To access Web form controls, you need to create an ASP.NET Web application. The steps to create an ASP.NET Web application are specified in the following list:

1.Launch Visual Studio .NET.

2.Click on File. The File menu will appear.

3.On the File menu, click on New and then click on Project. The New Project dialog box will appear.

4.In the New Project dialog box, click on Visual C# projects in the Project Types list.

5.Click on ASP.NET Web Application in the Templates list and specify a name and location for the Web application in the Location text box.

6.Click on OK. A new Web application will be created.

In an ASP.NET Web application, Web form controls are available in the Toolbox. Click the View menu and select Toolbox to open Toolbox. The Web form controls are shown in Figure 19-1.

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

437

 

 

 

 

FIGURE 19-1 Web form controls in Visual Studio .NET

As you can see, in the Toolbox, there are different categories of controls available for Web forms. These categories are described in Table 19-1.

Table 19-1 Types of Web Forms Controls

Web Forms Controls

Description

User controls

User controls are used to create reusable Web pages. You use user

 

controls to create controls that are reusable across multiple Web

 

pages. For example, you can use a user control to create reusable

 

menus items, tables,toolbars, and so on.

Validation controls

Validation controls are used to test the values that the user

 

specifies against the requirements defined by the programmer.

 

A validation control must be associated with another control

 

that accepts user input. For example, you can use a Required-

 

FieldValidator control to check whether the user has specified

 

a value for the control. You can also use RegularExpression-

 

Validator to check for a pattern of values that is entered by

 

the user.

continues

438

Project 4

CREATING AN AIRLINE RESERVATION PORTAL

 

 

 

 

 

 

Table 19-1 Types of Web Forms Controls (continued)

 

 

 

 

 

 

 

 

 

Web Forms Controls

Description

 

 

 

 

 

 

 

 

 

 

HTML server controls

HTML server controls are used to expose an object to a server

 

 

 

 

 

 

so that the object becomes accessible to the programmers. You

 

 

 

 

 

 

can then program these controls within an ASP.NET file.

 

 

Web form controls

 

Web form controls are used to create Web pages that have built-

 

 

 

 

 

 

in features that are more advanced than HTML Web pages. For

 

 

 

 

 

 

example, you can use label, text box, button, or other controls to

 

 

 

 

 

 

create a Web page. In addition, Web server controls include

 

 

 

 

 

 

advanced controls, such as Image, Calendar, and Table.

 

 

 

 

 

 

 

 

Y

 

 

 

 

 

 

 

 

L

 

 

In this section, I will discuss Web form controls and validation controls. These

 

 

controls will be used frequently in theFSkyShark Airlines application. The Web

 

 

form controls available in SP.NET with their respective descriptions are given

 

 

in Table 19-2.

 

 

M

 

 

 

 

 

 

 

 

A

 

 

 

 

Table 19-2 Web Form Controls

 

 

 

 

 

 

 

 

E

 

 

 

 

Control

 

Description

 

 

 

 

 

 

 

T

 

 

 

 

 

TextBox

 

Displays a text box in which users can enter text.

 

 

Label

 

 

Displays text that cannot be edited by the user. Commonly used to label

 

 

 

 

 

 

other controls on a Web form.

 

 

DropDownList

 

Allows users to select an option from a list of available options.

 

 

ListBox

 

Displays a list of options fr om which users can select multiple options.

 

 

Image

 

 

Displays a clickable or nonclickable image. A clickable image can be

 

 

 

 

 

 

used to provide a hyperlink to another Web form.

 

 

AdRotator

 

Displays a list of banners on the Web site.The list of banners can be

 

 

 

 

 

 

specified as an XML file.Each time a page is requested, banners are

 

 

 

 

 

 

retrieved from the file and displa yed sequentially.

 

 

CheckBoxList

 

Displays a group of check boxes. For example, you can have a

 

 

 

 

 

 

CheckBoxList control to accept a user ’s preferences for a par ty.

Team-Fly®

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

439

Table 19-2 Web Form Controls (continued)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Control

Description

 

 

 

 

 

 

 

 

 

RadioButtonList

Displays a list of radio buttons that allows users to select one option

 

 

from list of options. For example, you can use a RadioButtonList control

 

 

for gender, which displays two check boxes for Male and Female.

 

Calendar

Displays a calendar and allows users to select dates and weeks. You can

 

 

customize the appearance of the calendar to blend it with your Web

 

 

application.

 

 

 

 

LinkButton

A LinkButton control is similar to a Button control but it appears like a

 

 

hyperlink.

 

 

 

 

ImageButton

Displays a button on which you can display an image.

 

 

 

 

HyperLink

Used to create hyperlinks from one Web form to another.

 

 

 

 

Table

Creates a table and provides several useful methods and properties to

 

 

render a table from the programming logic of the application.

 

Panel

Creates a borderless division on the form that serves as a container for

 

 

other controls.

 

 

 

 

Repeater Control

Displays information from a dataset by using a set of HTML elements

 

 

and controls.The Repeater control repeats the HTML elements for

 

 

each record in the data set.

 

 

 

 

DataList

Provides extensive layout and formatting options to display information

 

 

in a table format.This control is similar to a Repeater control but offers

 

 

greater control over the format of the output.

 

 

 

 

DataGrid

The DataGrid control can retrieve information from a dataset and dis -

 

 

play it directly on a form in the table format without requiring a user to

 

 

specify the structure of data in the dataset.

 

 

 

 

 

 

 

 

 

 

ASP.NET provides a number of validation controls that simplify your task of validating user input. Instead of coding validation logic for each control, you can use the validation controls to validate information specified by a user. The validation controls of ASP.NET are summarized in Table 19-3.

440

Project 4

CREATING AN AIRLINE RESERVATION PORTAL

 

 

 

 

 

 

Table 19-3 Validation Controls

 

 

 

 

 

 

Control

Description

 

 

 

 

 

 

RequiredFieldValidator

Ensures that users specify a valid value in the control with

 

 

 

 

which the RequiredFieldValidator control is associated.

 

 

CompareValidator

Uses the comparison operators to validate user input with a

 

 

 

 

predefined value of another control or a database field.

 

 

RangeValidator

Validates the user input to determine whether or not it is in

 

 

 

 

a predefined range for numbers, characters, or dates.

 

 

RegularExpressionValidator

Matches user input with a regular expressions. For example,

 

 

 

 

it checks for predictable sequences of characters, such as

 

 

 

 

social security numbers, telephone numbers, and zip codes.

 

 

CustomValidator

Checks the user’s entry by using validation logic that you

 

 

 

 

code for your application.

 

 

 

 

 

Working with Web Form Server Controls

Each control has a set of properties that can be used for modifying its state. You can modify the properties of a control at design time or run time.

To modify the properties of a control at design time, follow these steps:

1.Right-click on a control and select Properties. The Properties window for the control will appear. For example, the Properties window of the ListBox control is shown in Figure 19-2.

2.Change the required property of the control. For example, you can change the ID of a list box to lstMonth.

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

441

 

 

 

 

FIGURE 19-2 The Properties window of a control

You can now modify the Items property of the list box to add months to the lstMonth control programmatically. To change the properties of a control at run time, you use the Code Editor window. Use the following steps to open the Code Editor window and change the properties of a control:

1.Drag a Button control from Toolbox to the form.

2.Double-click the button. The Code Editor window will open.

3.Add the following code to the Click event of the button.

private void Button1_Click(object sender, System.EventArgs e)

{

lstMonth.Items.Add(“January”); lstMonth.Items.Add(“February”); lstMonth.Items.Add(“March”); lstMonth.Items.Add(“April”); lstMonth.Items.Add(“May”); lstMonth.Items.Add(“June”);