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

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

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

428 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

performing reservations and cancellations, and network administrators perform administrative tasks pertaining to managing databases and user accounts.

The airline portal for SkyShark Airlines is based upon the roles of business executives.The database of SkyShark Airlines comprises eight user-defined tables that are associated with the interface of the portal. The interface of the portal presents different Web forms to business executives based upon their role in the airline.

 

 

Y

 

L

 

F

 

M

 

A

 

E

 

T

 

 

Team-Fly®

Chapter 19

Basics of ASP.NET

Web Applications

430 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

In the previous chapter, Chapter 18, “Project Case Study and Design,” you studied the case of SkyShark Airlines. You also saw the database schema and Web

forms that provide the interface to the application.

All the forms that you viewed in the last chapter are Web forms that are created in ASP.NET. The Web application ASP.NET is the next version of ASP (Active Server Pages) 3.0. However, ASP.NET is quite different from ASP because it includes a completely revamped ASP engine and uses the CLR (common language runtime) environment for running ASP code.

You will see in this chapter that ASP.NET simplifies Web development by allowing you to separate programming logic from the HTML code that is used to display the page. ASP.NET also provides improved caching and debugging support.

You can write programming logic in ASP.NET by using Visual Basic .NET or Visual C#. In this chapter, I will explore the basic concepts related to ASP.NET, which will help you to start creating the SkyShark Airlines project. The subsequent chapters will build on the concepts covered in this chapter and help you consolidate your learning of ASP.NET.

Visual Studio .NET provides a highly user friendly and powerful interface to simplify development in ASP.NET. For example, the ASP.NET Web application and ASP.NET Web service project templates can be conveniently used to develop Web applications and Web services. These templates provide the Web form and HTML controls that allow you to design a Web form without needing to write a single line of code. Another important aspect that I will explain in this chapter is the use of Visual Studio .NET for creating ASP.NET applications.

Getting Started with ASP.NET

ASP.NET is a server-side scripting technology that allows you to create dynamic Web sites. ASP.NET is built upon the .NET framework. In this section, I’ll explore the new features and the types of applications that you can create in ASP.NET. However, before that, I’ll examine the prerequisite software package that is required to create and run ASP.NET applications.

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

431

 

 

 

 

Prerequisites for ASP.NET Applications

ASP.NET is a component of .NET Framework SDK (Software Development Kit). ASP.NET can be downloaded free of cost from the Microsoft site at http://msdn.microsoft.com. The software requirements to install the SDK are specified in the following list:

Windows XP Server or Windows XP Professional

Windows 2000 Server or Windows 2000 Professional

Windows NT 4.0 with Service Pack 6a

IIS (Internet Information Server) 5.0 or a later version

In an enterprise environment, you have different development servers and deployment servers. The development servers are used to develop the applications, and the deployment servers are the Web servers on which the application is deployed. You need not install .NET Framework SDK on the deployment servers. Instead, you can install .NET Framework Redistributable, which is also downloadable from the Microsoft Web site.

New Features in ASP.NET

After having examined the prerequisites to run ASP.NET, I will examine the new features of ASP.NET. The most important features of ASP.NET are specified in the following list:

Compiled code. One of the most prominent differences between ASP and ASP.NET is that the code in ASP.NET is compiled while the code in ASP 3.0 is interpreted. The compilation of code significantly improves the performance of the Web application. It also allows early binding and strong typing of the program code.

Support for multiple programming languages. ASP.NET supports Visual Basic .NET, Visual C#, and JScript. You can use any of these three languages to write your code. You can also use a combination of all three languages to develop your Web application. The development team might code one module for the application in Visual Basic .NET and another in Visual C# and JScript.

Support for WYSIWYG (what you see is what you get) editors. Similar to ASP 3.0, ASP.NET applications can be coded in WYSIWYG

432 Project 4 CREATING AN AIRLINE RESERVATION PORTAL

HTML editors. In this project, the Visual Studio .NET development platform that you will use to develop the SkyShark Airlines project is a WYSIWYG editor.

Improved caching. ASP.NET optimizes the performance of request processing by providing extensive caching support. ASP.NET exposes a cache API to help programmers cache their own objects. This allows programmers to have greater control over caching of their content. The data in ASP.NET can be cached at two levels, page and fragment. Pagelevel caching enables you to cache a complete page, and fragment caching enables the caching of only a part of a page.

Extensive security. ASP.NET applications can use Windows, Forms, and Microsoft Passport authentication mechanisms. In ASP 3.0 applications, security is configured at IIS. ASP.NET takes the security model to the next step by allowing you to configure security at the IIS and Web application. You can also enable directory-level security for your Web application. I will explain ASP.NET security in detail later in this chapter and in Chapter 25, “Securing the Application.”

Debugging and tracing. An important task in application development is debugging and tracing. When you build your ASP.NET application in Visual Studio .NET, you can use the extensive debugging tools and tracing methods to debug your application.

Efficient state management. State management is the process of maintaining state and page information over multiple requests for the same or different pages. ASP.NET provides easy-to-use application and sessionstate capabilities. Session and application data can be stored in userdefined objects, which can be updated or queried from time to time.

Improved data access. The ADO.NET architecture provides a reliable and efficient mode of accessing data. Although the topic of ADO.NET is too complicated to cover in a single chapter, I will use the ADO data objects extensively in this project.These objects are explained as they are used.

After having examined the features of ASP.NET, you can now examine the applications that can be created in ASP.NET.

BASICS OF ASP.NET WEB APPLICATIONS

Chapter 19

433

 

 

 

 

Types of ASP.NET Applications

InASP.NET, you can create two types of applications: Web applications and Web services. In this section, I include a brief description of these applications.

ASP.NETWeb Applications

ASP.NET Web applications are applications that interact directly with users through the Internet. The Web sites that you commonly browse on the Internet are Web applications. The Web applications that are built using ASP.NET are ASP.NET Web applications. I will explain ASP.NET Web applications in this project in detail.

ASP.NETWeb Services

Consider a scenario where you need to consolidate the data that is available on three data sources and display it on a Web site.The only way to display output by using Web applications is to create different Web applications and provide links to all applications on a Web site. However, consider that the data that you need to access may be stored on legacy systems and the data may be relevant only when it is consolidated and analyzed.

Implementing such a scenario by using Web applications can be a challenging task. Here, the solution is in ASP.NET Web services. Web services expose the functionality provided by one application to other applications. The functionality exposed by a Web service can be implemented by other Web services or applications in a number of ways, depending upon the business requirements of Web service clients. Therefore, a Web service that exposes a product catalog can be displayed on one Web application as a list and on another Web application with the custom search functionality.

Another advantage offered by Web services is that they are platform-independent. Data is exposed by Web services in an XML format, which is the industry standard.Therefore, any device that can interpret XML can make use of the data that is available from the Web service. I will discuss more of Web services in the next professional project, “Project 5: Creating a Web Portal for a Bookstore.”

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