ASP.net Session 13

Embed Size (px)

Citation preview

  • 7/30/2019 ASP.net Session 13

    1/24

    Slide 1 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    In this session, you will learn to:

    Handle and log errors

    Debug Web applications

    Implement Web parts in a Web application

    Objectives

  • 7/30/2019 ASP.net Session 13

    2/24

    Slide 2 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 eventlog, 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 theGlobal.asax file is executed.

    Logging Errors

  • 7/30/2019 ASP.net Session 13

    3/24

    Slide 3 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    A log can be created by using the EventLog class of the

    .NET Framework.

    The following table describes the various properties of theEventLog class.

    Logging Errors (Contd.)

    Proper ty Descr ipt ionEntries Used to retrieve the contents from an event log.

    Log Used to specify the name of the log from where the log information is to

    be read or written.

    Source 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.

  • 7/30/2019 ASP.net Session 13

    4/24

    Slide 4 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    The following table describes the various methods of theEventLog class:

    Logging Errors (Contd.)

    Method Descr ipt ion

    Exists 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.

    SourceExists 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.

    CreateEventSourc

    e

    Used to establish an application as a valid event source for adding

    information to a log.

    WriteEntry Used to write information to an event log.

  • 7/30/2019 ASP.net Session 13

    5/24

    Slide 5 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 customerror page, you need to log the error in a log file.

    Activity 8.1: Implementing Page-Level Error Handling

  • 7/30/2019 ASP.net Session 13

    6/24

    Slide 6 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    Solution:

    To display a custom error page and to log the error in a log

    file, you need to perform the following steps:

    1. Modify the Global.asax file.

    2. Add a new Web page.

    3. Design the new Web page.

    4. Modify the web.config file.

    5. Modify the AddAlbumDetails.aspx file

    6. Execute the application.

    7. Verify the logged error.

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

  • 7/30/2019 ASP.net Session 13

    7/24Slide 7 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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.

    Debugging Web Applications

  • 7/30/2019 ASP.net Session 13

    8/24Slide 8 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 throughthe statements in an application to ensure that the execution

    path and the data are correct.

    Debugging Web Applications (Contd.)

  • 7/30/2019 ASP.net Session 13

    9/24Slide 9 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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

    element of the web.config file to

    true, as

    shown in the following code snippet:

    Configuring Web Applications for Debugging

  • 7/30/2019 ASP.net Session 13

    10/24Slide 10 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    You can attach a debugger to client-side scripts.

    Client-side debugging works only with Internet Explorer.

    Debugging Client-Side Scripts

    Let us see how to debug client-side scripts

  • 7/30/2019 ASP.net Session 13

    11/24Slide 11 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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.

    Implementing Web Parts in a Web Application

  • 7/30/2019 ASP.net Session 13

    12/24Slide 12 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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.

    Advantages of Using Web Parts

  • 7/30/2019 ASP.net Session 13

    13/24Slide 13 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 theWeb page.

    The different types of display modes available are:

    Browse

    Edit

    DesignCatalog

    Connect

    Web Part Modes

  • 7/30/2019 ASP.net Session 13

    14/24Slide 14 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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.

    Web Parts Control Set

    Property Descript ion

    WebPartManager Manages all the Web part controls on a page. Each Web

    part page contains exactly one WebPartManager control.

    CatalogZone 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.

    EditorZone Contains EditorPart controls and is used to enable a user

    to edit and personalize Web part controls on a page.

    WebPartZone Contains the overall layout for the Web Part controls that

    compose the main user interface of a page.ConnectionsZone Contains Connection controls.

    CatalogPart Contains a list of available Web part controls that a user

    can add to a page.

    Connection Creates a connection between two Web part controls on a

    page.

    EditorPart Serves as the base class for specialized controls that

    provide the functionality to edit Web parts.

  • 7/30/2019 ASP.net Session 13

    15/24Slide 15 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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.

    Web Parts Control Set (Contd.)

  • 7/30/2019 ASP.net Session 13

    16/24Slide 16 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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;

    }

    Web Parts Control Set (Contd.)

  • 7/30/2019 ASP.net Session 13

    17/24Slide 17 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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, youneed 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.

    Activity 9.1: Creating a Web Parts Page

  • 7/30/2019 ASP.net Session 13

    18/24Slide 18 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    Solution:

    To create the Web parts page, you need to perform the

    following tasks:

    1. Create a user control.

    2. Add a new Web page.

    3. Design the new Web page.

    4. Test the application.

    Activity 9.1: Creating Web Parts Page(Contd.)

  • 7/30/2019 ASP.net Session 13

    19/24Slide 19 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 acommunication 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.

    Connecting Web Parts

  • 7/30/2019 ASP.net Session 13

    20/24Slide 20 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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

    Connecting Web Parts (Contd.)

  • 7/30/2019 ASP.net Session 13

    21/24Slide 21 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    To create connected Web parts, you need to perform the

    following tasks:

    1. Create the provider Web part as a custom control.

    2. Create the consumer Web part as a custom control

    3. Add the Web parts to the Toolbox.

    4. 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 element inside the

    element.

    Connecting Web Parts (Contd.)

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

  • 7/30/2019 ASP.net Session 13

    22/24

    Slide 22 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 Webapplications are:

    Breakpoints

    Stepping

    Data Viewing

    Summary

  • 7/30/2019 ASP.net Session 13

    23/24

    Slide 23 of 24Ver. 1.0

    Developing Web Applications Using ASP.NET

    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 Web-

    based information.

    The advantages of using Web parts in a website are:

    Web parts enable users to personalize the content of a Webpage.

    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

    Summary (Contd.)

  • 7/30/2019 ASP.net Session 13

    24/24

    Developing Web Applications Using ASP.NET

    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.

    Summary (Contd.)