Sie sind auf Seite 1von 24

Developing Web Applications Using ASP.

NET
Objectives

In this session, you will learn to:


Handle and log errors Debug Web applications Implement Web parts in a Web application

Ver. 1.0

Slide 1 of 24

Developing Web Applications Using ASP.NET


Logging Errors

It is possible that certain unexpected errors occur in a Web application while it is running in a real-world environment. It would be easy to resolve such errors if a log of these errors is maintained. ASP.NET enables you to log unhandled errors in an event log, database, or some other file. To log errors occurring in an application, you need to create a log for the application in the event log. Whenever an unhandled error occurs in a Web application, the Application_Error event handler written in the Global.asax file is executed.

Ver. 1.0

Slide 2 of 24

Developing Web Applications Using ASP.NET


Logging Errors (Contd.) A log can be created by using the EventLog class of the .NET Framework. The following table describes the various properties of the EventLog class.
Property Entries Log Source Description Used to retrieve the contents from an event log. Used to specify the name of the log from where the log information is to be read or written. Used to specify the name by which the application needs to be registered in the event log. The name specified as the Source property should be unique in an event log. A single event log can contain content from multiple sources.

Ver. 1.0

Slide 3 of 24

Developing Web Applications Using ASP.NET


Logging Errors (Contd.)

The following table describes the various methods of the EventLog class:
Method Exists Description Used to determine whether the log to which the information is to be written or read exists on the specified computer. If the computer is not specified, the existence of the log is checked on the local computer. Used to determine whether a particular source name already exists on the specified computer. If the computer is not specified, the existence of the event source is checked on the local computer. Used to establish an application as a valid event source for adding information to a log. Used to write information to an event log.

SourceExists

CreateEventSourc e WriteEntry

Ver. 1.0

Slide 4 of 24

Developing Web Applications Using ASP.NET


Activity 8.1: Implementing Page-Level Error Handling

Problem Statement:
When a user clicks the Add New Album Details button on the AddAlbumDetails page, the application throws a default error. As a developer, you need to ensure that a custom error page is displayed whenever an error occurs on the AddAlbumDetails page. In addition to displaying the custom error page, you need to log the error in a log file.

Ver. 1.0

Slide 5 of 24

Developing Web Applications Using ASP.NET


Activity 8.1: Implementing Page-Level Error Handling (Contd.)

Solution:
To display a custom error page and to log the error in a log file, you need to perform the following steps:
1. 2. 3. 4. 5. 6. 7. Modify the Global.asax file. Add a new Web page. Design the new Web page. Modify the web.config file. Modify the AddAlbumDetails.aspx file Execute the application. Verify the logged error.

Ver. 1.0

Slide 6 of 24

Developing Web Applications Using ASP.NET


Debugging Web Applications

Debugging is the process of finding and fixing errors in an application. An application may contain syntax errors, logical errors, and run-time errors. The syntax errors are resolved during the compilation of the Web application. The logical errors and run-time errors cannot be caught during compilation. Such errors require you to debug the code while it is running.

Ver. 1.0

Slide 7 of 24

Developing Web Applications Using ASP.NET


Debugging Web Applications (Contd.)

There are various tools available that help you debug your Web application. One such tool is Visual Debugger available in Visual Studio IDE. The debugging tools enable you to step line-by-line through the statements in an application to ensure that the execution path and the data are correct.

Ver. 1.0

Slide 8 of 24

Developing Web Applications Using ASP.NET


Configuring Web Applications for Debugging

To enable debugging in your ASP.NET Web application, you must configure the application to compile into a debug build. You can configure a Web application to compile into a debug build by setting the debug attribute in the <compilation> element of the web.config file to true, as shown in the following code snippet:
<configuration> <system.web> <compilation debug="true" /> </system.web> </configuration>

Ver. 1.0

Slide 9 of 24

Developing Web Applications Using ASP.NET


Debugging Client-Side Scripts

You can attach a debugger to client-side scripts. Client-side debugging works only with Internet Explorer.
Let us see how to debug client-side scripts

Ver. 1.0

Slide 10 of 24

Developing Web Applications Using ASP.NET


Implementing Web Parts in a Web Application

A Web part:
Is a modular unit that can contain any type of Web-based information, which could be a picture, static text, or database information. Enables a user to create a personalized user interface and enables you to present information in a better way. Enables you make your websites interactive.

Ver. 1.0

Slide 11 of 24

Developing Web Applications Using ASP.NET


Advantages of Using Web Parts

The advantages of using Web parts in a website are:


Web parts enable users to personalize the content of a Web page. Web parts can be assigned role-based access, thereby, determining which Web part can be shared by all users or which should be hidden for certain roles. Web parts can be connected to each other. One of the connected Web parts is defined as a provider and the other as a consumer.

Ver. 1.0

Slide 12 of 24

Developing Web Applications Using ASP.NET


Web Part Modes

Personalization is one of the main advantages of using Web parts. You can implement personalization by allowing a user to view a Web page in several display modes. Display modes allow users to modify or personalize the Web page. The different types of display modes available are:
Browse Edit Design Catalog Connect

Ver. 1.0

Slide 13 of 24

Developing Web Applications Using ASP.NET


Web Parts Control Set

ASP.NET provides you with the Web parts control set that is designed to implement Web parts in your websites. Some of the Web part controls available in ASP.NET are described in the following table.
Property WebPartManager CatalogZone Description Manages all the Web part controls on a page. Each Web part page contains exactly one WebPartManager control. Contains CatalogPart controls and is used to create a catalog of Web part controls from which a user can select a Web part control to add to a page. Contains EditorPart controls and is used to enable a user to edit and personalize Web part controls on a page. Contains the overall layout for the Web Part controls that compose the main user interface of a page. Contains Connection controls. Contains a list of available Web part controls that a user can add to a page. Creates a connection between two Web part controls on a page. Serves as the base class for specialized controls that provide the functionality to edit Web parts.

EditorZone WebPartZone ConnectionsZone CatalogPart Connection EditorPart

Ver. 1.0

Slide 14 of 24

Developing Web Applications Using ASP.NET


Web Parts Control Set (Contd.)

Once you have created a Web part page, you can allow users to switch between the various Web part display modes. To enable a user to switch between the various modes, you can include a drop-down list on the Web page that allows a user to select the desired mode.

Ver. 1.0

Slide 15 of 24

Developing Web Applications Using ASP.NET


Web Parts Control Set (Contd.)

You can then type the following code snippet in the SelectedIndexChanged event of the DropDownList control:
WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(Page); string selectedMode = DropDownList1.SelectedValue; WebPartDisplayMode mode = wpm.SupportedDisplayModes[selectedMode]; if (mode != null) { wpm.DisplayMode = mode; }

Ver. 1.0

Slide 16 of 24

Developing Web Applications Using ASP.NET


Activity 9.1: Creating a Web Parts Page

Problem Statement:
The management of MusicMania wants its website to provide a customizable user interface to all its users. The users should be able to hide, show, and arrange the components on the home page according to their requirements. As the first step towards implementing this requirement, you need to create a Web parts page, Portal.aspx. The Portal.aspx page should include a Web part that displays the availability status of the MusicMania database. Prerequisite: Ask your faculty to provide you the required starter files.

Ver. 1.0

Slide 17 of 24

Developing Web Applications Using ASP.NET


Activity 9.1: Creating Web Parts Page(Contd.)

Solution:
To create the Web parts page, you need to perform the following tasks:
1. 2. 3. 4. Create a user control. Add a new Web page. Design the new Web page. Test the application.

Ver. 1.0

Slide 18 of 24

Developing Web Applications Using ASP.NET


Connecting Web Parts

There are situations where you need to connect sets of data from different data sources. Web part connections are based on a pull model, where the consumer pulls data from the provider. To create a connection, the provider control defines a communication contract in the form of an interface, indicating the data it can provide. The consumer control knows about this contract and retrieves the data through the connection.

Ver. 1.0

Slide 19 of 24

Developing Web Applications Using ASP.NET


Connecting Web Parts (Contd.)

To participate in a connection, a Web part needs to define a connection point. Connection points are of two types:
Provider connection points Consumer connection points

Ver. 1.0

Slide 20 of 24

Developing Web Applications Using ASP.NET


Connecting Web Parts (Contd.)

To create connected Web parts, you need to perform the following tasks:
1. 2. 3. 4. Create the provider Web part as a custom control. Create the consumer Web part as a custom control Add the Web parts to the Toolbox. Add the Web part controls from the Toolbox to a Web part page and assign an ID and title to each of the two controls. 5. Add the connection information to the WebPartManager control by opening the Web part page in the Source view and adding a <staticConnections> element inside the <asp:WebPartManager> element.

Let us see how to create and use a Web user control

Ver. 1.0

Slide 21 of 24

Developing Web Applications Using ASP.NET


Summary

In this session, you learned that:


ASP.NET enables you to log unhandled errors in an event log, database, or some other file. An event log can be created by using the EventLog class of the .NET Framework. Debugging is the process of finding and fixing errors in an application. There are various tools available that help you debug your Web application. One such tool is Visual Debugger available in Visual Studio IDE. The features of a debugger that help you debug Web applications are:
Breakpoints Stepping Data Viewing

Ver. 1.0

Slide 22 of 24

Developing Web Applications Using ASP.NET


Summary (Contd.)

The aspnet_wp.exe process is attached to the debugger to debug a running page. A Web part is a modular unit that can contain any type of Webbased information. The advantages of using Web parts in a website are:
Web parts enable users to personalize the content of a Web page. Web parts can be assigned role-based access, thereby determining which Web parts can be shared by all users or which should be hidden for certain roles. Web parts can be connected to each other.

The different types of display modes of a Web part are:


Browse mode Edit mode Design mode Catalog mode Connect mode
Ver. 1.0

Slide 23 of 24

Developing Web Applications Using ASP.NET


Summary (Contd.)

A Web page that is composed of Web parts is known as a Web part page. A Web part page is divided into zones that provide the structure for placing Web parts.

Ver. 1.0

Slide 24 of 24

Das könnte Ihnen auch gefallen