Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects

.pdf
Скачиваний:
475
Добавлен:
12.02.2016
Размер:
14.7 Mб
Скачать

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”);

442 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

lstMonth.Items.Add(“July”);

lstMonth.Items.Add(“August”);

lstMonth.Items.Add(“September”);

lstMonth.Items.Add(“October”);

lstMonth.Items.Add(“November”);

lstMonth.Items.Add(“December”);

}

After specifying the preceding code, when you run the application and click on the button, the list box is populated with the months of the year.

Configuring ASP.NET Applications

After you create an ASP.NET application, you need to secure it. You also need to ensure that your application can be ported to Web ser vers easily. Therefore, two important features of configuring an ASP.NET application are security and deployment. I will include a brief description of these concepts in this section.

Configuring Security for ASP.NET Applications

ASP.NET applications can be secured at IIS or at the Web application level. The security methods employed at these two levels are described in the following list:

IIS. You can configure application-level security to specify the authentication mode for a Web site or a virtual directory at IIS. You can also configure the file access permissions for the Web site on IIS Server.

ASP.NET. All ASP.NET applications include a Web.Config file that is used for storing the application configuration. You can modify this file for changing the authentication mode of your application, specifying a list of users who are allowed to access your Web site, and specifying the default login page that is displayed when an unauthenticated user requests for a resource that requires authentication. The file-based security mechanism provided by ASP.NET can help implement subdirectory level security for a Web application. For example, you can implement form-based authentication for Web forms in one folder of your application, which is accessible to the registered users on the Web site. You can implement Windows authentication for Web forms in another folder, which is accessible only by corporate employees.

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

 

443

 

 

 

 

 

 

You will learn how to implement different security mechanisms for your Web application in Chapter 25.

Deploying ASP.NET Applications

You can deploy ASP.NET applications by copying the files in the virtual directory of an application to a virtual directory on the destination server. However, Visual Studio .NET provides a more sophisticated method of deployment. Instead of manually copying all ASP.NET files, you can use the Web Setup deployment project in Visual Studio .NET.

The Web Setup deployment project is a project template that can be configured to accomplish the necessary tasks to deploy a Web application. Some tasks that you can configure using the Web Setup project template are specified in the following list:

Check for the presence of .NET run-time files and other prerequisite software before installing the application.

Prompt the user for the name for the virtual directory in which the application should be installed.

Enforce business rules, such as acceptance of user agreements, before the installation of the software.

Create databases and add data that might be necessary for the successful execution of your application.

It is advisable to use the Web Setup project template for deploying your ASP.NET applications. However, another easy method to deploy your application is to use the Copy Project feature in Visual Studio .NET. This feature copies the source files of the application to a virtual directory that you specify. You can use this feature only when the computer on which you want to deploy your application is accessible on the network. It is not possible to use it to distribute your application to customers or business partners. I will describe the steps to deploy ASP.NET applications in Chapter 26, “Deploying the Application.”

Creating a Sample ASP.NET Application

After having examined the basic concepts of an ASP.NET application, you can build on your knowledge by attempting a simple ASP.NET application. In this

444 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

section, I have created a simple application that queries a username and password in a database and displays a welcome message if the user is successfully authenticated.

Creating a New Project

The first step in creating an ASP.NET application in Visual Studio .NET is to add a new project by using the ASP.NET Web Application template. The steps to add a new ASP.NET Web Application were discussed in the section “Summary of Web Form Server Controls.” Create a new project with the name SampleApplication. After creating the new project, proceed to the next section, “Adding Controls to the Project,” to add controls to the sample application.

Adding Controls to the Project

To design the user interface of the application, you need to add controls to the application. The steps to add controls to the Web form are specified in the following list:

1.Click on the View menu and select Toolbox to open Toolbox.

2.Drag a Label control from Toolbox to the default form in the Web application.

3.Change the properties of the label as given here:

ID=lblCaption

Text=Please log on

Font

Bold=True

Italic=True

Name=Georgia

4.Drag two label controls to the form for accepting the username and the password. Change the Text property of these label controls to User Name and Password, respectively.

5.Drag two TextBox controls to the form and change their ID to txtUserName and txtPassword, respectively.

6.Drag a Button control to the form and change its Text property to Submit. In addition, change the ID of the button to btnSubmit.

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

445

 

 

 

 

The basic structure of the form is complete. Next, you need to add validation controls to the form to validate user input before data is processed on the server. To add validation controls to the form, follow these steps:

1.Drag a RequiredFieldValidator control to the form for validating the User Name text box. Change the properties of the RequiredFieldValidator control as mentioned in the following list:

ErrorMessage=Invalid user name

ControlToValidate=txtUserName

2.Drag another RequiredFieldValidator control to the form for validating

the Password text box. Change the properties of the control as mentioned here:

ErrorMessage=Invalid password

ControlToValidate=txtPassword

The interface of the form is complete. However, you can add one more Label control to display a welcome message if the user logs on successfully. Change the ID of the label to lblMessage and clear the Text property. The complete form is shown in Figure 19-3.

FIGURE 19-3 Form to accept username and password

After having designed the interface of the form, you can code the functionality of the Web application.

446 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

Coding the Application

To code the functionality of the application, you need to create the database structure and use the database to validate users. In this section, I will explain the procedure for creating a database and utilizing it in the application.

Creating the Database

To validate the username and the password, I have created a database named SampleDatabase and added a Logon table to the database. Next, I added two records to the Logon table. To create a similar structure for your Web application, execute the SQL script given as follows:

CREATE DATABASE SampleDatabase

GO

USE SampleDatabase

GO

CREATE TABLE Logon (

[UserName] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Password] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL

) ON [PRIMARY] GO

ALTER TABLE Logon WITH NOCHECK ADD

CONSTRAINT [PK_Logon] PRIMARY KEY CLUSTERED

(

[UserName] ) ON [PRIMARY]

GO

INSERT INTO LOGON

VALUES (‘John’, ‘password’)

GO

INSERT INTO LOGON

VALUES (‘Suzan’, ‘mypassword’)

GO

Adding Functionality to the Application

ASP.NET includes data access tools that make it easier for you to interact with databases. The SQL Server .NET data provider is used for accessing SQL Server databases. The data provider provides three primary classes to access databases:

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

 

447

 

 

 

 

 

 

SqlConnection. The SqlConnection class is used for creating a connection to the database.

SqlDataAdapter. The SqlDataAdapter class is used for adding, updating, deleting, and selecting records from the database.

DataSet. The DataSet class is used to cache data that is retrieved from a database. A DataSet object comprises a number of DataTable objects that contain data retrieved from database tables.

In addition to the three classes described here, ASP.NETprovides the SqlCommand class that can be used for executing queries on a database.

Visual Studio .NET provides data controls that correspond to the SQL Server data provide classes described in the previous list. These controls are available on the Data tab of the Toolbox. Follow these steps to use the data controls for accessing databases:

1.Drag an SqlDataAdapter control from Toolbox to the form. Data Adapter Configuration Wizard will start.

2.On the Welcome screen, click on Next. The Choose Your Data Connection screen of the wizard will appear.

3.On the Choose Your Data Connection screen, click on New Connection. The Data Link Properties dialog box will appear, as shown in Figure 19-4.

FIGURE 19-4 The Data Link Properties dialog box