Anda di halaman 1dari 29

Developing Web Applications Using ASP.

NET
Objectives In this session, you will learn to:
Implement server controls

Ver. 1.0

Slide 1 of 29

Developing Web Applications Using ASP.NET


Implementing Server Controls 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 and events 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.

Ver. 1.0

Slide 2 of 29

Developing Web Applications Using ASP.NET


Implementing Server Controls (Contd.) 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.

Ver. 1.0

Slide 3 of 29

Developing Web Applications Using ASP.NET


Implementing Server Controls (Contd.) Advantages of using server controls are:
Automatic state maintenance Browser independent rendering Easily programmable model

Ver. 1.0

Slide 4 of 29

Developing Web Applications Using ASP.NET


Processing of Server Controls on a Web Page When a page is requested for the first time, the server controls are processed in the following sequence:
1. 2. 3. 4. 5. 6. 7. Initializing Loading PreRendering Saving Rendering Disposing Unloading

Ver. 1.0

Slide 5 of 29

Developing Web Applications Using ASP.NET


Processing of Server Controls on a Web Page (Contd.) When the Web page is posted back to the Web server, the server controls are processed in the following sequence:
1. 2. 3. 4. 5. 6. 7. 8. 9. Initializing Loading view state Loading control instance Loading the postback data PreRendering Saving Rendering Disposing Unloading

Ver. 1.0

Slide 6 of 29

Developing Web Applications Using ASP.NET


Types of Server Controls ASP.NET provides the following types of server controls:
HTML server controls Web server controls Validation controls

Ver. 1.0

Slide 7 of 29

Developing Web Applications Using ASP.NET


Types of Server Controls (Contd.) 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 server controls with their description.
HTML Server Controls HtmlAnchor control HtmlInputText control HtmlInputCheckBox control HtmlInputRadioButton control HtmlSelect control Description Works as the HTML <a> tag but runs on the server. It is used to direct the user from one page to another. Works as the HTML <INPUT type="text"> tag but runs on the server. It is used to gather information from the user. Works as the HTML <INPUT type="checkbox"> tag but runs on the server. It is useful to implement questions that can have only two answers, such as yes/no or true/false Works as the HTML <INPUT type="radio"> tag but runs on the server. You can use the HtmlInputRadioButton control to implement multiple-choice questions, where a user can select only one option. Works as the HTML <select> 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. Slide 8 of 29

Ver. 1.0

Developing Web Applications Using ASP.NET


Types of Server Controls (Contd.) 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 control Label control ListBox control CheckBox and CheckBoxList control RadioButton and RadioButtonList control AdRotator control Calendar control DropDownList control ImageButton control

Ver. 1.0

Slide 9 of 29

Developing Web Applications Using ASP.NET


Types of Server Controls (Contd.)
ImageButton control Button control Wizard control MultiView control HyperLink control FileUpload control XML control Substitution control

Ver. 1.0

Slide 10 of 29

Developing Web Applications Using ASP.NET


Types of Server Controls (Contd.) 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 are entered by the user:
RequiredFieldValidator control RegularExpressionValidator control CompareValidator control RangeValidator control ValidationSummary control CustomValidator control

Ver. 1.0

Slide 11 of 29

Developing Web Applications Using ASP.NET


Server Control Events 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.

Ver. 1.0

Slide 12 of 29

Developing Web Applications Using ASP.NET


Server Control Events (Contd.) 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.

Ver. 1.0

Slide 13 of 29

Developing Web Applications Using ASP.NET


Server Control Events (Contd.) 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.

Let us see how to create a default event handler

Ver. 1.0

Slide 14 of 29

Developing Web Applications Using ASP.NET


Server Control Events (Contd.) 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. Let us see how to create a non-default event handler

Ver. 1.0

Slide 15 of 29

Developing Web Applications Using ASP.NET


Server Control Events (Contd.) 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 eventhandling procedures having well-defined names and signatures. Event wire-ups determine the procedures that need to be called 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 .aspx pages is set to true, as shown in the following code snippet:
<%@ Page Language=C#AutoEventWireup=true%>

Ver. 1.0

Slide 16 of 29

Developing Web Applications Using ASP.NET


Server Control Events (Contd.)
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 handlers explicitly.

Ver. 1.0

Slide 17 of 29

Developing Web Applications Using ASP.NET


Server Control Events (Contd.) 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 the variable, casting it to the appropriate type. Examining the ID property of the variable to determine which object raised the event.

Ver. 1.0

Slide 18 of 29

Developing Web Applications Using ASP.NET


Loading Controls Dynamically There are situations where you need to add controls dynamically to a Web page. You can add the controls at run time. By adding controls to a Web page at run time makes a website flexible, interactive, and user-friendly. When adding a control dynamically to a Web page, you need to place them in a container, so as to make the Web page well-structured.

Ver. 1.0

Slide 19 of 29

Developing Web Applications Using ASP.NET


Loading Controls Dynamically (Contd.) 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.

Ver. 1.0

Slide 20 of 29

Developing Web Applications Using ASP.NET


Adding Event Handlers Dynamically 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.

Ver. 1.0

Slide 21 of 29

Developing Web Applications Using ASP.NET


Activity 2.1: Creating a Web Application in 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 selling music 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.

Ver. 1.0

Slide 22 of 29

Developing Web Applications Using ASP.NET


Activity 2.1: Creating a Web Application in ASP.NET (Contd.) 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.

Ver. 1.0

Slide 23 of 29

Developing Web Applications Using ASP.NET


Implementing Generic Handler 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 the IsReusable property. The ProcessRequest method is responsible for processing individual HTTP requests. The IsReusable property specifies whether the IHttpHandlerFactory object can put the handler in a pool and reuse it to increase performance. The IHttpHandlerFactory object is the object that calls the appropriate handler.
Let us see how to create a generic handler
Ver. 1.0

Slide 24 of 29

Developing Web Applications Using ASP.NET


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

Ver. 1.0

Slide 25 of 29

Developing Web Applications Using ASP.NET


Summary (Contd.)
The sequence in which a server control is processed when the page is requested for the first time is:
1. 2. 3. 4. 5. 6. 7. Initializing Loading PreRendering Saving Rendering Disposing Unloading

Ver. 1.0

Slide 26 of 29

Developing Web Applications Using ASP.NET


Summary (Contd.)
The sequence in which a server control is processed when the Web page is posted back to the Web browser is:
1. 2. 3. 4. 5. 6. 7. 8. 9. Initializing Loading view state Loading control instance Loading the postback data PreRendering Saving Rendering Disposing Unloading

Ver. 1.0

Slide 27 of 29

Developing Web Applications Using ASP.NET


Summary (Contd.)
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 object Event object

The two types of events in ASP.NET server control event model are:
Postback events Nonpostback events
Ver. 1.0

Slide 28 of 29

Developing Web Applications Using ASP.NET


Summary (Contd.)
The two controls that act as container when adding controls dynamically are:
PlaceHolder Panel

Ver. 1.0

Slide 29 of 29

Anda mungkin juga menyukai