02 asp.net session02

19
Slide 1 of 19 Developing Web Applications Using ASP.NET In this session, you will learn to: Describe various event-handling techniques Explain how to detect browser types and capabilities Explain how to access page headers Describe how to handle page-level errors and application-level errors Implement advanced techniques for handling events Implement browser-capability detection Implement page-header manipulation Implement page-level and application-level error handling Objectives

Upload: vivek-chan

Post on 14-Apr-2017

95 views

Category:

Education


0 download

TRANSCRIPT

Page 1: 02 asp.net session02

Slide 1 of 19

Developing Web Applications Using ASP.NET

In this session, you will learn to:Describe various event-handling techniquesExplain how to detect browser types and capabilitiesExplain how to access page headersDescribe how to handle page-level errors and application-level errorsImplement advanced techniques for handling eventsImplement browser-capability detectionImplement page-header manipulationImplement page-level and application-level error handling

Objectives

Page 2: 02 asp.net session02

Slide 2 of 19

Developing Web Applications Using ASP.NET

ASP.NET provides you with a flexible framework that enables you to work with event handlers in several ways.The various approaches that can be used to work with the event handlers include:

Using default eventsUsing non-default eventsUsing the AutoEventWireup capabilities of a Web form to associate events and event-handling methodsCreating centralized event-handling methods to respond to multiple events

Event Handling in Web Applications

Page 3: 02 asp.net session02

Slide 3 of 19

Developing Web Applications Using ASP.NETDefault and Non-Default Events

ASP.NET objects usually expose an event that is designated as the default event.In addition to a default event, many ASP.NET objects also expose other events, called non-default events.

Page 4: 02 asp.net session02

Slide 4 of 19

Developing Web Applications Using ASP.NET

Non-default event handlers are used to respond to the non-default events.Each event has a specific signature associated with it.

Non-Default Event Handlers

Page 5: 02 asp.net session02

Slide 5 of 19

Developing Web Applications Using ASP.NET

Event wire-ups determine the procedures that need to be called when objects raise events.The AutoEventWireUp property of the .aspx pages should be set to true to indicate that procedures with well-defined names and signatures are used as event handlers.By default, the AutoEventWireUp property of the .aspx pages is set to true.

<%@ Page Language=“C#” AutoEventWireup=“True”%>

Event Wire-Ups

Page 6: 02 asp.net session02

Slide 6 of 19

Developing Web Applications Using ASP.NET

Centralized event handlers run in response to multiple events.This helps in creating code that is easier to maintain.

Centralized Event Handlers

Page 7: 02 asp.net session02

Slide 7 of 19

Developing Web Applications Using ASP.NETHow to Determine Which Web Server Control Raised an Event

To determine which control caused the event, you need to perform the following steps:

In the event handler, declare a variable with a type that matches the control that raised the event.Assign the sender argument of the event handler to the variable, casting it to the appropriate type.Examine the ID property of the variable to determine which object raised the event.

Page 8: 02 asp.net session02

Slide 8 of 19

Developing Web Applications Using ASP.NET

When a Web browser makes a request for a Web page, it sends information that describes the browser in the Hypertext Transfer Protocol (HTTP) header.You can query the information sent by the browser by using code in the ASP.NET Web page.Detecting the browser capability ensures that the response the application sends to the browser is appropriate.Much of the information sent by the Web browser is encapsulated as properties in the Request.Browser object.

Browser Capability Detection

Page 9: 02 asp.net session02

Slide 9 of 19

Developing Web Applications Using ASP.NET

The header section of the HTML script contains metadata for the page such as title and styles used in the page.The metadata is useful in search engines for categorizing the Web pages.The information in the page header can be used at run time by the server-side code.The page header information can be changed at run time.ASP.NET exposes each Web page to your code as a System.Web.UI.Page object.You can use the properties of the Page.Header object, such as the Page.Header.Title property, to query and set its values at run time.

Page Header Retrieval

Page 10: 02 asp.net session02

Slide 10 of 19

Developing Web Applications Using ASP.NETHow to Pass Values Between ASP.NET Web Pages

You can pass information between pages in various ways:Use a query string that appends the information to the URL of the target pageExpose the data as public properties on the source page

Page 11: 02 asp.net session02

Slide 11 of 19

Developing Web Applications Using ASP.NETThe HttpServerUtility.Transfer Method

The HttpServerUtility.Transfer method performs the following functions:

Halts the code running on the current Web pageRequests a different Web page to carry on the processingExample:

Server.Transfer("Productdisplay.aspx?productname=bike&color=blue");

Page 12: 02 asp.net session02

Slide 12 of 19

Developing Web Applications Using ASP.NET

ASP.NET enables you to handle run time errors with: Structured exception handling:

It enables you to handle exceptions in your Web applications by using Try…Catch blocks.

Page-level error handling:It enables you to trap all the otherwise-unhandled server-side errors on the page. Page_Error event of the Page object enables you to trap all the unhandled exceptions in a page.

Application-level error handling:It enables you to trap all the otherwise-unhandled server-side errors in the Web application.There are two standard approaches you can follow when implementing an application-level error handler:

Create Application_Error event method in global.asax fileInclude a <customErrors> element in the Web.config file

Page-Level and Application-Level Error Handling

Page 13: 02 asp.net session02

Slide 13 of 19

Developing Web Applications Using ASP.NETHandling Application-Level Errors Using <customErrors> Element

Using the <customErrors> elements requires you to modify the web.config file of your web application.Refer to the following code snippet:<system.web>  <customErrors

defaultRedirect="errorhandler.aspx“ mode="On"> <error statusCode="403”

redirect=“Page1.htm"/>    <error statusCode="404”

redirect=“Page2.htm" />   </customeErrors></system.web>

Page 14: 02 asp.net session02

Slide 14 of 19

Developing Web Applications Using ASP.NET

Problem Statement: You are a developer in the Adventure Works organization, a fictitious bicycle manufacturer. You have been asked to assist in the development of the Business-to-Consumer (B2C) Web application and a Business-to-Employee (B2E) extranet portal.Decisions on the design of the application have already been taken. You have been asked to carry out a number of specific tasks in order to implement various elements of this design.

Demo: Programming a Web Application

Page 15: 02 asp.net session02

Slide 15 of 19

Developing Web Applications Using ASP.NET

As part of the first phase of the B2C development, you have been asked to complete prototypes for the following pages:

• Feedback.aspx. You will create a centralized event handler for the Click event of two Button objects.

• Contact.aspx. You will create an event handler for the non-default Command event of Button objects.

• Diagnostics.aspx. You will retrieve properties of the Browser object and display them on the Web page. You will also access the Page.Header object.

• TrailReport.aspx. You will implement a page-level error handler that deals with all run-time errors that can occur on this Web page.

You will also modify the Web.config file to enable application-level error handling by redirecting all otherwise-unhandled exceptions to the customErrors.aspx page.

Demo: Programming a Web Application (Contd.)

Page 16: 02 asp.net session02

Slide 16 of 19

Developing Web Applications Using ASP.NET

Solution:To solve this problem, you need to perform the following tasks:

1. Implement Non-Default Event Handlersa.Open the Adventure Works Web site.b.Create a centralized event handler for two Button controls.c. Specify the feedback_Click method as the Click event handler for

the feedback buttons.d.Create an event handler for the Command event of Button controls.e.Specify the SortGroup_Command method as the Command event

handler for the Button controls.f. Test the Web site functionality.

Demo: Programming a Web Application (Contd.)

Page 17: 02 asp.net session02

Slide 17 of 19

Developing Web Applications Using ASP.NET

2. Detect Browser Capabilities and Set Page Header Propertiesa.Review the Diagnostics.aspx page.b.Detect browser properties.c. Display browser properties.d.Modify the page title.e.Test the Web site functionality.

3. Handle Page-Level Exceptionsa.Handle page-level exceptions.b.Handle exceptions at the application level.c. Test exception handling.

Demo: Programming a Web Application (Contd.)

Page 18: 02 asp.net session02

Slide 18 of 19

Developing Web Applications Using ASP.NET

In this session, you learned that:ASP.NET objects usually expose an event that is designated as the default event. In addition to the default event, ASP.NET objects expose other additional events known as non-default events.When you want to write code that responds to a non-default event, you need to define an event handler for it.Event wire-ups are the mechanism that ASP.NET uses to determine which procedures to call when objects raise events.By default, the AutoEventWireUp attribute for .aspx pages is set to true.When a Web browser makes a request for a Web page, it sends information that describes the browser in the Hypertext Transfer Protocol (HTTP) header.

Summary

Page 19: 02 asp.net session02

Slide 19 of 19

Developing Web Applications Using ASP.NET

Centralized event handlers run in response to multiple events.ASP.NET provides a robust and flexible error-handling framework. It enables you to handle run-time errors with:

Structured exception handlingPage-level error handlersApplication-level error handlers

Summary (Contd.)