asp.net session 02

Upload: prerana-tokas

Post on 14-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 ASP.net Session 02

    1/29

    Slide 1 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    In this session, you will learn to:

    Implement server controls

    Objectives

  • 7/30/2019 ASP.net Session 02

    2/29

    Slide 2 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    In the earlier days of Web development, the programmers

    had to use HTML controls to design a Web application.

    The HTML controls do not provide any built-in methods and

    events.

    Programmers had to manually write the methods andevents using client-side scripts to create a dynamic Web

    page.

    This is very time consuming. In addition, it is insecure

    because the client-side scripts are visible to users when

    they view the source for the Web page.ASP.NET solves the problems associated with the use of

    HTML controls by providing server controls.

    Implementing Server Controls

  • 7/30/2019 ASP.net Session 02

    3/29

    Slide 3 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Server controls:

    Are the fundamental building blocks that are specifically

    designed to work with Web forms in an ASP.NET application.

    Can be rendered as markup text on a Web browser.

    Are capable of executing the program logic on the server.Provide built-in methods and events eliminating the need for

    writing the events and methods manually.

    Provide security to the application by hiding the program logic

    from the user.

    Provide an object-oriented programmable interface.

    Can be viewed as special HTML tags, which are processed by

    the server.

    Implementing Server Controls (Contd.)

  • 7/30/2019 ASP.net Session 02

    4/29

    Slide 4 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Advantages of using server controls are:

    Automatic state maintenance

    Browser independent rendering

    Easily programmable model

    Implementing Server Controls (Contd.)

  • 7/30/2019 ASP.net Session 02

    5/29

    Slide 5 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    When a page is requested for the first time, the server

    controls are processed in the following sequence:

    1. Initializing

    2. Loading

    3. PreRendering4. Saving

    5. Rendering

    6. Disposing

    7. Unloading

    Processing of Server Controls on a Web Page

  • 7/30/2019 ASP.net Session 02

    6/29

    Slide 6 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    When the Web page is posted back to the Web server, the

    server controls are processed in the following sequence:

    1. Initializing

    2. Loading view state

    3. Loading control instance4. Loading the postback data

    5. PreRendering

    6. Saving

    7. Rendering

    8. Disposing9. Unloading

    Processing of Server Controls on a Web Page (Contd.)

  • 7/30/2019 ASP.net Session 02

    7/29Slide 7 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    ASP.NET provides the following types of server controls:

    HTML server controls

    Web server controls

    Validation controls

    Types of Server Controls

  • 7/30/2019 ASP.net Session 02

    8/29Slide 8 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    HTML server controls:

    Are processed by the Web server.

    Make full use of the .NET Framework utilities such as

    validation.

    The following table lists some of the common HTML servercontrols with their description.

    Types of Server Controls (Contd.)

    HTML Server Controls Description

    HtmlAnchor control Works as the HTML tag but runs on the server. It is used to

    direct the user from one page to another.

    HtmlInputText

    control

    Works as the HTML tag but runs on the

    server. It is used to gather information from the user.

    HtmlInputCheckBoxcontrol

    Works as the HTML tag but runs onthe server. It is useful to implement questions that can have onlytwo answers, such as yes/no or true/false

    HtmlInputRadioButton

    control

    Works as the HTML tag but runs on the

    server. You can use the HtmlInputRadioButton control to implementmultiple-choice questions, where a user can select only one option.

    HtmlSelect control Works as the HTML element but it runs on the server. It

    is used to create a list box and when a user has to select an option

    from a list of options.

  • 7/30/2019 ASP.net Session 02

    9/29Slide 9 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Web server controls:

    Are created on the server and require a runat=server

    attribute to work.

    Some of the most commonly used Web server controls are:

    TextBox controlLabel control

    ListBox control

    CheckBox and CheckBoxList control

    RadioButton and RadioButtonList control

    AdRotator controlCalendar control

    DropDownList control

    ImageButton control

    Types of Server Controls (Contd.)

  • 7/30/2019 ASP.net Session 02

    10/29Slide 10 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    ImageButton control

    Button control

    Wizard control

    MultiView control

    HyperLink controlFileUpload control

    XML control

    Substitution control

    Types of Server Controls (Contd.)

  • 7/30/2019 ASP.net Session 02

    11/29Slide 11 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Validation controls:

    Are used for validating the values entered by the users in other

    controls.

    ASP.NET provides the following validation controls that can

    be attached to input controls to validate the values that areentered by the user:

    RequiredFieldValidator control

    RegularExpressionValidator control

    CompareValidator control

    RangeValidator controlValidationSummary control

    CustomValidator control

    Types of Server Controls (Contd.)

  • 7/30/2019 ASP.net Session 02

    12/29Slide 12 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    ASP.NET server control event model:

    In ASP.NET, events associated with the server controls

    originate on the client but the processing of the events

    happens only on the server.

    ASP.NET supports the following two types of events:

    Postback events: In postback events, a Web page is sent back to

    the server for processing.

    Nonpostback events: In nonpostback events, a Web page does

    not immediately cause a postback.

    Server Control Events

  • 7/30/2019 ASP.net Session 02

    13/29Slide 13 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Event handlers:

    When an event is raised, the corresponding event handler is

    searched and the code written in the event handler is

    executed.

    The various approaches that can be used to work with event

    handlers include:

    Using default events.

    Using non-default events.

    Using the AutoEventWireup capabilities of a Web form.

    Creating centralized event-handlers.

    Server Control Events (Contd.)

  • 7/30/2019 ASP.net Session 02

    14/29Slide 14 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Using default events:

    A default event is an event that is most commonly associated

    with a control.

    Default events are handled by a default event handler.

    Server Control Events (Contd.)

    Let us see how to create a default event handler

  • 7/30/2019 ASP.net Session 02

    15/29Slide 15 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Using non-default events:

    In addition to the default event, each control exposes other

    events, called non-default events.

    Non-default events are handled by non-default event handlers.

    Server Control Events (Contd.)

    Let us see how to create a non-default event handler

  • 7/30/2019 ASP.net Session 02

    16/29Slide 16 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Using the AutoEventWireup capabilities of a Web form:

    ASP.NET provides the event wire-up capability to a Web form

    using which events can be automatically associated with event-

    handling procedures having well-defined names and

    signatures.

    Event wire-ups determine the procedures that need to becalled when objects raise events.

    To enable the event wire-up capability of a Web form, you

    need to set the AutoEventWireUp attribute of the .aspx page

    to true.

    By default, the AutoEventWireUp attribute of the .aspxpages is set to true, as shown in the following code snippet:

    Server Control Events (Contd.)

  • 7/30/2019 ASP.net Session 02

    17/29Slide 17 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    The AutoEventWireup attribute can have the following two

    values:

    True: This means that the ASP.NET runtime does not require

    events to specify event handlers, such as Page_Load and

    Page_Init.

    False: This means that the events need to specify event handlersexplicitly.

    Server Control Events (Contd.)

  • 7/30/2019 ASP.net Session 02

    18/29Slide 18 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Creating centralized event-handlers:

    A centralized event handler eliminates the need for declaring

    separate event handlers for different ASP.NET objects.

    You can create one centralized event handler that can perform

    appropriate action at runtime.

    When you create a single event handler to respond to multiple

    events that should trigger different actions, you need to

    determine which event invoked the event handler by:

    Declaring a variable in the event handler with a type that matches

    the control that raised the event.

    Assigning the sender argument of the event handler to thevariable, casting it to the appropriate type.

    Examining the ID property of the variable to determine which

    object raised the event.

    Server Control Events (Contd.)

  • 7/30/2019 ASP.net Session 02

    19/29

  • 7/30/2019 ASP.net Session 02

    20/29Slide 20 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Two controls that act as containers when adding controls

    dynamically are:

    PlaceHolder: Is used as a container that is not visible in a Web

    application. It reserves a place in the structure of the Web

    page for other controls that are added dynamically

    Panel: Renders div tag around all the child controls in the

    panel, which makes it useful when you want to apply a css

    class to a container.

    Loading Controls Dynamically (Contd.)

  • 7/30/2019 ASP.net Session 02

    21/29Slide 21 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    An event handler for a control is usually created at design

    time.

    It can also be created dynamically at run time for the

    controls that are created dynamically.

    Adding Event Handlers Dynamically

  • 7/30/2019 ASP.net Session 02

    22/29Slide 22 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Problem Statement:

    MusicMania is a music store with its branches located all over

    the United States. It has a huge collection of music albums to

    cater to the tastes of all kind of music lovers. To increase the

    sales, the management at MusicMania has decided to hire a

    team of professionals to develop a Web application for sellingmusic online.

    You, being a part of the team, have been asked to create a

    file-system Web application. Initially, you are required to

    design the Home page and the About Us page.

    Activity 2.1: Creating a Web Application in ASP.NET

  • 7/30/2019 ASP.net Session 02

    23/29

    Slide 23 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    Solution:

    To design the home page of the MusicMania Web application,

    you need to perform the following tasks:

    1. Design the Home page.

    2. Design the About Us page.

    3. Execute the application.

    Activity 2.1: Creating a Web Application in ASP.NET (Contd.)

  • 7/30/2019 ASP.net Session 02

    24/29

    Slide 24 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    A generic handler can be used when you want to display

    the contents of a text file, an XML file, or an image.

    The generic handler provides a class that implements the

    IHttpHandler interface.

    The class provides the ProcessRequest method and theIsReusable property.

    The ProcessRequest method is responsible for

    processing individual HTTP requests.

    The IsReusable property specifies whether the

    IHttpHandlerFactory object can put the handler in apool and reuse it to increase performance.

    The IHttpHandlerFactory object is the object that calls

    the appropriate handler.

    Implementing Generic Handler

    Let us see how to create a generic handler

  • 7/30/2019 ASP.net Session 02

    25/29

    Slide 25 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    In this session, you learned that:

    Server controls are the fundamental building blocks that are

    specifically designed to work with Web forms in an ASP.NET

    application.

    Server controls provide:

    Automatic state maintenance.

    Browser independent rendering.

    Easily programmable model.

    Summary

  • 7/30/2019 ASP.net Session 02

    26/29

    Slide 26 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    The sequence in which a server control is processed when the

    page is requested for the first time is:

    1. Initializing

    2. Loading

    3. PreRendering

    4. Saving5. Rendering

    6. Disposing

    7. Unloading

    Summary (Contd.)

  • 7/30/2019 ASP.net Session 02

    27/29

    Slide 27 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    The sequence in which a server control is processed when the

    Web page is posted back to the Web browser is:

    1. Initializing

    2. Loading view state

    3. Loading control instance

    4. Loading the postback data5. PreRendering

    6. Saving

    7. Rendering

    8. Disposing

    9. Unloading

    Summary (Contd.)

  • 7/30/2019 ASP.net Session 02

    28/29

    Slide 28 of 29Ver. 1.0

    Developing Web Applications Using ASP.NET

    The various types of server controls provided by ASP.NET can

    be classified into the following categories:

    HTML server controls

    Web server controls

    Validation controls

    User controls

    An event is a message that contains details about the user

    action, such as the cause of the event and the control on which

    the event occurred.

    The two arguments passed by all events in ASP.NET are:

    Sender objectEvent object

    The two types of events in ASP.NET server control event

    model are:

    Postback events

    Nonpostback events

    Summary (Contd.)

  • 7/30/2019 ASP.net Session 02

    29/29

    Developing Web Applications Using ASP.NET

    The two controls that act as container when adding controls

    dynamically are:

    PlaceHolder

    Panel

    Summary (Contd.)