You are on page 1of 19

Module 4

Adding Functionality to a Microsoft ASP.NET Web Form

Module 4: Adding Functionality to a Microsoft ASP.NET Web Form


Working with Code-Behind Files Handling Server Control Events

Creating Classes and Components by Using Visual Studio

2008 SP1

Handling Page Events

Lesson 1: Working with Code-Behind Files


Methods for Implementing Code Mixed Code and Inline Code

Code-Behind Files
Using Code-Behind Files

Methods for Implementing Code

The three methods for implementing code:

Mixed code: Place the code in the same file as the content Inline code: Place the code in a separate section of the content file Code-behind file: Place the code in a separate file

The code-behind file is the default method for adding code in Visual Studio 2008 SP1

Mixed Code and Inline Code


Mixed code is known as embedded code blocks In inline code, the markup and code are in separate sections of a

single file

Mixed code

Inline code

Single file <content> <code> <content>

Single file

<content> <code>

Code-Behind Files
Code-behind files contain the programming logic for a single Web
Form

Separate files

<content>

<code>

Using Code-Behind Files

To use code-behind files for adding functionality for Web Forms:

Associate each .aspx page with a code-behind file

Compile the code-behind file

Precompile the application

Lesson 2: Handling Server Control Events


What Are Event Handlers? What Are Client-Side Event Handlers?

What Are Server-Side Event Handlers?


How Client-Side and Server-Side Event Handlers Are

Processed

Creating Server-Side Event Handlers Demonstration: How To Create Server-Side Event

Handlers Handlers

Working with Web Server Controls by Using Event

What Are Event Handlers?


An event handler is the action that is performed in response to a generated event

Are usually triggered by the user

Events

Are handled by event handlers on the

client side or server side

What Are Client-Side Event Handlers?


Client-side event handlers handle events that are raised on the client computer
User Interface

Postback

Server-side process

Client-side function captures event

What Are Server-Side Event Handlers?


Server-side event handlers are used to handle events that are generated

from both Web and HTML server controls

Response Client Event handler processes the events

Request Server-side process

How Client-Side and Server-Side Event Handlers Are Processed


The The client information requests is an ASP.NET validatedWeb on the Forms client page from side, the andWeb thenserver. sent to the server, where server-side The server returns a page processing takes place containing markup and script to the client. For The server repeats the example, this page validation and stores the includes a TextBox control information from the text and a Submit button. The box in a database page also contains clientside script that validates Server-side resources can the contents of the text be used for other data box. processing because the client-side The user enters script invalid does not information in the text access server resources. box, and the client-side script generates and displays an error message. The user corrects the information in the text box, and then clicks the Submit button.

Plese enter your birth date:


December 32, 31, 3000 1980 December

Submit

URL Request

Value

Client

Value HTML & Script

Server

Creating Server-Side Event Handlers


Create the control that generates the event on the Web Form

Provide the code for the event handler in the codebehind file that handles the event

Link the event handler to the control event

What Are Types, Components, and Classes?

Types

Components

Classes

Types is a common name for value types or structures and reference types or objects

Components consist of one or more types that are compiled into a DLL assembly

Classes are reference types and are used as templates for objects

Lesson 4: Handling Page Events


Page Event Life Cycle The PostBack Process

Handling Postbacks
Demonstration: How To Handle Page Events

Page Event Life Cycle


Page event life cycle refers to the series of events that occur when

an ASP.NET page is requested

1. Page initialization 2. Load 3. Validation 4. Postback event handling


Page request Web server

5. Rendering 6. Page Unloading

The PostBack Process


time when the a user requests The The first server updates label a Instead, the event handlers forfor page from the server, the test to reflect the change and
Select Your Country AutoPostBack=True
Page1.aspx Page1.aspx Page1.aspx Page1.aspx Page1.aspx <form id="form1" <form id="form1" <form <form id="form1" id="form1" <<form id="form1" runat="server"> runat="server"> runat="server"> runat="server"> runat="server"> <asp:ListBox> <asp:ListBox <asp:ListBox> <asp:ListBox> <asp:ListBox ... AutoPostBack="true"> ... ... AutoPostBack="true"> </asp:ListBox> ... </asp:ListBox> </asp:ListBox> ... <asp:Label </asp:ListBox> <asp:Label <asp:Label Text="" Text="" </asp:ListBox> Text="Canada" /> <asp:Label Text="" /> /> Text="Canada" /><asp:Button <asp:Button /> <asp:Label Text="Italy" <asp:Button <asp:Button Text="Submit" Text="Submit" /> /> /><asp:Button Page1.aspx.vb Text="Submit" /> Text="Submit" /> </form> </form> Text="Submit" /></form> </form> </form> Sub Page_Load() If Not Page.IsPostBack Then ' Fill the list box ... End If End Sub

the controls on updated the form (the list Page.IsPostBack in the Page_Load then sends the box and the button) run. In this event succeeds and in the information back to the the code client scenario, the box event the block runs. In list this example, handler changes the label code populates a list box. to

Brazil
Canada Italy
Canada Italy

Page1.aspx.vb Sub Page_Load() Sub If Page_Load() Not Page.IsPostBack If Not Then Page.IsPostBack ' Fill the list box Then ... ' Fill the list box ... End If End Sub

reflect the new list box election. The The server server then then returns returns the the page updated information to the client. to the user. In this example, the The user the same page, page has views a list box, a blank label, but the label has now changed to and a Submit button on it. reflect the list box selection. When If you the want the new value user changes theof the list box to be sent theand server selection in the listto box, immediately, and you do not then clicks the Submit button, want to wait for the user to click the information is sent back to the server. Submit button, you can set the the list box controls The server can determine that AutoPostBack property to True. this is a page that is being posted With the AutoPostBack property back to itself. Therefore, the test set to true, as soon as the user for Page.IsPostBack in the changes the selection in the list Page_Load event fails and box, the information is sentthe to

Submit

Listbox.SelectedIndex=1

Listbox.SelectedIndex=2

Client

Server

code in the block does not run. the server.

Handling Postbacks
Page_Load fires on every request: Use Page.IsPostBack to run conditional logic Page.IsPostBack prevents reloading for each postback
The following code samples show how the Page.IsPostBack property is used [Visual Basic] Protected Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then End If End Sub [Visual C#] protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { } }

Module Review
Review Questions Real-World Issues and Scenarios

Best Practices

You might also like