Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event Handling & Viewstate CS 351 Ed Gellenbeck. Today Review of Web Forms ASP.NET Object Hierarchy Events State Management Page State Session State Application.

Similar presentations


Presentation on theme: "Event Handling & Viewstate CS 351 Ed Gellenbeck. Today Review of Web Forms ASP.NET Object Hierarchy Events State Management Page State Session State Application."— Presentation transcript:

1 Event Handling & Viewstate CS 351 Ed Gellenbeck

2 Today Review of Web Forms ASP.NET Object Hierarchy Events State Management Page State Session State Application State

3 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 blocks of code that are processed on the server The first time a Web form is requested, the entire page is compiled. Subsequent requests are served from this compiled page. Contains HTML and Web Controls

4 What’s the big deal? Web Forms are Object-oriented Event-based Efficient (compiled) Provide automatic state management

5 ASP.NET Object Hierarchy The Web Page is an object With properties, methods, and events page.IsPostBack, page.User, page.FindControl(),... Web Controls are objects With properties, methods, and events Have access to the entire.NET Framework class hierarchy With properties, methods, and events

6 Web Control Hierarchy Control WebControl ButtonTextBoxLabel TemplateControl PageUserControl... ID Page Visible Font Width Height Text Rows Columns TextRequest Response IsPostBack

7 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;... } Properties name of the control nested controls enclosing control page to which the control belongs should the control be visible? state of this control (see later) should the state be persistent? Methods does the control have nested controls? searches for a nested control with the name id loads data from a data source loads the state from the request stream saves the state to the response stream renders the control to HTML Events after the control was created after the state was loaded from the request after DataBind was called before the control is rendered to HTML before the control is released

8 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

9 <asp:Button Text="..." OnClick="DoClick" Runat="sever" /> Event-based Processing mouse click void DoClick (object sender, EventArgs e) {... } ClientServer event handler click event

10 ControlEventWhen does the event occur? allInit Load PreRender Unload when the control is created after the data that were sent by the browser have been loaded into the control before HTML code for this control is generated before the control is removed from memory ButtonClickwhen the button was clicked TextBoxTextChangedwhen the contents of the TextBox changed CheckBoxCheckedChangedwhen the state of the CheckBox changed ListBoxSelectedIndexChangedwhen a new item from the list has been selected Kinds of Events

11 Round Trip of a Web Page Click ClientServer round trip event + page state 1. Creation create page object and its controls Page Label TextBox Button

12 ClientServer 2. Initialisation -raise Init events Init Click round trip event + page state Page Label TextBox Button

13 ClientServer 3. Loading -load controls with the values that the user has entered (page state) -raise Load events Load Click round trip event + page state Page Label TextBox Button

14 ClientServer 4. Action handle event(s) (Click, TextChanged,...) Page Label TextBox Button

15 ClientServer 5. Rendering -raise PreRender events -call Render methods of all controls, which render the controls to HTML PreRender...... + page state HTML Page Label TextBox Button

16 ClientServer 6. Unloading -raise Unload events for cleanup actions Unload...... Page Label TextBox Button

17 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" />

18 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

19 ASP.NET State Management Page state saving: ViewState["counter"] = counterVal; reading: int counterVal = (int) ViewState["counter"]; Session state saving:Session["cart"] = shoppingCart; reading:DataTable shoppingCart = (DataTable) Session["cart"]; Application state saving:Application["database"] = databaseName; reading: string databaseName = (string) Application["databaseName"];


Download ppt "Event Handling & Viewstate CS 351 Ed Gellenbeck. Today Review of Web Forms ASP.NET Object Hierarchy Events State Management Page State Session State Application."

Similar presentations


Ads by Google