220 likes | 426 Views
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. ASP Web Form.
E N D
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
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 WebControls
So what’s the big deal? • Web Forms are • Object-oriented • Event-based • Efficient (compiled) • Provide some automatic state management
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
Web Control Hierarchy Control ID Page Visible ... WebControl TemplateControl Font Width Height ... Page UserControl Request Response IsPostBack ... Button TextBox Label Text Text Rows Columns Text _Default Request Response IsPostBack
Control Class 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 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; ... }
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
Event-based Processing mouse click click event event handler void DoClick (object sender, EventArgs e) { ... } <asp:Button Text="..." OnClick="DoClick" Runat="sever" /> Client Server
Round Trip of a Web Page round trip event Page + page state Label TextBox Click Button 1. Creation create page object and its controls Client Server
round trip event Init Page + page state Init Label Init TextBox Click Button Init 2. Initialisation - raise Init events Client Server
round trip event Load Page + page state Load Label Load TextBox Click Button Load 3. Loading - load controls with the values that the user has entered (page state) - raise Load events Client Server
Page Label TextBox Button 4. Action handle event(s) (Click, TextChanged, ...) Client Server
PreRender Page PreRender Label PreRender TextBox HTML <html> ... <input type="text" ...> <input type="button" ...> ... </html> Button PreRender + page state 5. Rendering - raise PreRender events - call Render methods of all controls, which render the controls to HTML Client Server
Unload Page Unload Label Unload TextBox <html> ... <input type="text" ...> <input type="button" ...> ... </html> Button Unload 6. Unloading - raise Unload events for cleanup actions Client Server
AutoPostBack(causes a delayed event to lead to an immediate round trip) <asp:TextBox Runat="server" AutoPostBack="true" OnTextChanged="DoTextChanged" /> TextChanged Round trip events(cause an immediate round trip) <asp:Button Text="click me" Runat="server" OnClick="DoClick" /> Click Delayed events(are handled at the next round trip) <asp:TextBox Runat="server" OnTextChanged="DoTextChanged" /> TextChanged
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
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"];