1 / 19

Event Handling & State Management

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.

Download Presentation

Event Handling & State Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Event Handling & State Management 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 a reference to a code-behind file • Contains HTML and WebControls

  4. So what’s the big deal? • Web Forms are • Object-oriented • Event-based • Efficient (compiled) • Provide some automatic state management

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

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

  7. 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; ... }

  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. Event-based Processing mouse click click event event handler void DoClick (object sender, EventArgs e) { ... } <asp:Button Text="..." OnClick="DoClick" Runat="sever" /> Client Server

  10. Kinds of Events

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

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

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

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

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

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

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

  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"];

More Related