Top Banner
Event Handling & State Management CS 351 Ed Gellenbeck
19

Event Handling & State Management CS 351 Ed Gellenbeck.

Jan 14, 2016

Download

Documents

Opal Wilkins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Event Handling & State Management CS 351 Ed Gellenbeck.

Event Handling & State Management

CS 351

Ed Gellenbeck

Page 2: Event Handling & State Management CS 351 Ed Gellenbeck.

Today

Review of Web Forms ASP.NET Object Hierarchy Events State Management

Page State Session State Application State

Page 3: Event Handling & State Management CS 351 Ed Gellenbeck.

ASP Web Form

A Web Form is a text file containing markup that can be edited in a simple text editor such as notepad. Contains a page directive

Global settings for the page Contains a reference to a code-behind file

Contains HTML and Web Controls

Page 4: Event Handling & State Management CS 351 Ed Gellenbeck.

So what’s the big deal?

Web Forms are Object-oriented Event-based Efficient (compiled) Provide some automatic state management

Page 5: Event Handling & State Management CS 351 Ed Gellenbeck.

ASP.NET Object Hierarchy

The Web Page is an object With properties, methods, and eventsPage.IsPostBack, Page.User, Page.FindControl(), ...

Web Controls are objects With properties, methods, and events

As objects, they have access to the entire .NET Framework class hierarchy With properties, methods, and events

Page 6: Event Handling & State Management CS 351 Ed Gellenbeck.

Web Control HierarchyControl

WebControl

Button TextBox Label

TemplateControl

Page UserControl

...

...

...

IDPageVisible

FontWidthHeight

Text TextRowsColumns

Text

RequestResponseIsPostBack

_DefaultRequestResponseIsPostBack

Page 7: Event Handling & State Management CS 351 Ed Gellenbeck.

Control Class

public class Control: ... {public virtual string ID { get; set; }public virtual ControlCollection Controls { get; }public virtual Control Parent { get; }public virtual Page Page { get; set; }public virtual bool Visible { get; set; }protected virtual StateBag ViewState { get; }public virtual bool EnableViewState { get; set; }...

public virtual bool HasControls();public virtual Control FindControl (string id);public virtual void DataBind();protected virtual void LoadViewState (object state);protected virtual object SaveViewState();protected virtual Render (HtmlTextWriter w);...

public event EventHandler Init;public event EventHandler Load;public event EventHandler DataBinding;public event EventHandler PreRender;public event EventHandler Unload;...

}

Propertiesname of the controlnested controlsenclosing controlpage to which the control belongsshould the control be visible?state of this control (see later)should the state be persistent?

Methodsdoes the control have nested controls?searches for a nested control with the name idloads data from a data sourceloads the state from the request streamsaves the state to the response streamrenders the control to HTML

Eventsafter the control was createdafter the state was loaded from the requestafter DataBind was calledbefore the control is rendered to HTMLbefore the control is released

Page 8: Event Handling & State Management CS 351 Ed Gellenbeck.

Events

Client-side Typically implemented with JavaScript and

handled in the browser Server-side

Slower because it involves round-trip to the server

More powerful because methods have access to server resources

Page 9: Event Handling & State Management CS 351 Ed Gellenbeck.

<asp:ButtonText="..."OnClick="DoClick"Runat="sever" />

Event-based Processingmouse click

void DoClick (object sender, EventArgs e) {...

}

Client Server

event handlerclick event

Page 10: Event Handling & State Management CS 351 Ed Gellenbeck.

Control Event When does the event occur?

all InitLoad

PreRender

Unload

when the control is created after the data that were sent by the

browser have been loaded intothe control

before HTML code for this controlis generated

before the control is removedfrom memory

Button Click when the button was clicked

TextBox TextChanged when the contents of the TextBoxchanged

CheckBox CheckedChanged when the state of the CheckBox changed

ListBox SelectedIndexChanged when a new item from the list has been selected

Kinds of Events

Page 11: Event Handling & State Management CS 351 Ed Gellenbeck.

Round Trip of a Web Page

Click

Client Server

round trip event

+ page state

1. Creationcreate page object and its controls

Page

Label

TextBox

Button

Page 12: Event Handling & State Management CS 351 Ed Gellenbeck.

Client Server

2. Initialisation- raise Init events

Init

Init

Init

Init

Click

round trip event

+ page statePage

Label

TextBox

Button

Page 13: Event Handling & State Management CS 351 Ed Gellenbeck.

Client Server

3. Loading- load controls with the values that the user

has entered (page state)- raise Load events

Load

Load

Load

Load

Click

round trip event

+ page statePage

Label

TextBox

Button

Page 14: Event Handling & State Management CS 351 Ed Gellenbeck.

Client Server

4. Actionhandle event(s)(Click, TextChanged, ...)

Page

Label

TextBox

Button

Page 15: Event Handling & State Management CS 351 Ed Gellenbeck.

Client Server

5. Rendering- raise PreRender events- call Render methods of all controls, which

render the controls to HTML

PreRender

PreRender

PreRender

PreRender

<html>...<input type="text" ...><input type="button" ...>...

</html>

+ page state

HTML

Page

Label

TextBox

Button

Page 16: Event Handling & State Management CS 351 Ed Gellenbeck.

Client Server

6. Unloading- raise Unload events for cleanup actions

Unload

Unload

Unload

Unload

<html>...<input type="text" ...><input type="button" ...>...

</html>

Page

Label

TextBox

Button

Page 17: Event Handling & State Management CS 351 Ed Gellenbeck.

Round trip events (cause an immediate round trip)

Click <asp:Button Text="click me" Runat="server"OnClick="DoClick" />

Delayed events (are handled at the next round trip)

TextChanged <asp:TextBox Runat="server"OnTextChanged="DoTextChanged" />

AutoPostBack (causes a delayed event to lead to an immediate round trip)

TextChanged <asp:TextBox Runat="server"AutoPostBack="true"OnTextChanged="DoTextChanged" />

Page 18: Event Handling & State Management CS 351 Ed Gellenbeck.

State Management

HTTP is a stateless protocol Each Web request is acted upon as a

independent action, with no previous history or data taken into account

Most Server-Side technologies have some mechanism to save page session application state data

Page 19: Event Handling & State Management CS 351 Ed Gellenbeck.

ASP.NET State Management

Page statesaving: ViewState["counter"] = counterVal;

reading: int counterVal = (int) ViewState["counter"];

Session statesaving: Session["cart"] = shoppingCart;

reading: DataTable shoppingCart = (DataTable) Session["cart"];

Application statesaving: Application["database"] = databaseName;

reading: string databaseName = (string) Application["databaseName"];