Life of a ASP.NET page - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Life of a ASP.NET page

Description:

The concept of state management, when applied to a Web application, refers to ... be used as a mechanism for storing generic client-specific state between post ... – PowerPoint PPT presentation

Number of Views:460
Avg rating:3.0/5.0
Slides: 16
Provided by: haimin8
Category:
Tags: asp | net | life | mechanism | page

less

Transcript and Presenter's Notes

Title: Life of a ASP.NET page


1
Life of a ASP.NET page ViewState
  • In ASP. Net

2
State Management
  • The concept of state management, when applied to
    a Web application, refers to the application's
    capability to remember the values a user enters
    across multiple pages, or screens.
  • Developers use state management techniques to
    store information between separate page requests.

3
State and HTTP
  • The primary protocol of the web, HTTP, is by
    nature stateless.
  • Web servers are designed to serve one page at a
    time, as quickly as possible. As soon as the Web
    server presents a requested page to a client, all
    communication with that client ends by default
    until he or she requests another page.

4
State Management Types in ASP.Net
  • There are 5 types of state management techniques
    available with ASP.Net
  • Query Strings
  • Cookies
  • View State
  • Session State
  • Application State

5
ViewState
  • View state are hidden fields when the form is
    posted to store state on behalf of a client.
  • Each time a page is posted to itself, the
    contents of the __VIEWSTATE field are sent as
    part of the post which means viewState will help
    to pass value across multiple posts to the same
    page, but not across different pages.
  • The primary use of view state is for controls to
    retain their state across post-backs, but it can
    also be used as a mechanism for storing generic
    client-specific state between post-backs to the
    same page.
  • You can safely access the ViewState in your Load
    event handler (In page_Load, get values from
    ViewState and assign to processing variables).

6
ViewState
  • ViewState variables are key/value pairs
  • There is no declaration of a viewState variable,
    you use them directly by giving a name and value
  • e.g., viewState(counter) intCounter
  • e.g., viewState(names) nameArray
  • ViewState variables are case sensitive
  • Before processing, you need to get the preserved
    values back from viewState variables
  • e.g., intCounter viewState(counter)
  • e.g., nameArray viewState(names)

7
Two Steps
  • One, putting the value away to a viewState
    variable
  • viewState (key) variable to preserve
  • Two, getting the value back from viewState
    variable before processing
  • Variable Name viewState (key)
  • The place (timing) of preserving and getting back
    variable values understand the processing of a
    page.

8
User types in URL in browser
Http Request
Page is compiled and sent first time in session
Http Response
User enters input and presses a button on the page
Http Request
Page is compiled and resent
Http Response
Updated Page
9
Stages of Page Processing
Http Request
Page_Init
Http Request
Page is compiled and sent first time in session
Page_Load
Http Response
Control Event
Http Request
Page is compiled and resent
Page_PreRender
Http Response
Http Response
Page_Unload
10
Page Events
  • Page_Init, the first step in a page lifecycle.
    ASP.NET utilizes this event for processing posted
    form data to restore controls to the state they
    were in on the previous request. ViewState cannot
    be accessed in this event.
  • Page_Load, at this point, the controls have been
    restored, used to notify the server control to
    perform any processing steps that are set to
    occur on each page request. You can access view
    state information in this event.
  • Page_Prerender, occurs after all execution
    happens and just before the page is posted back
    to the client, makes a convenient place to save
    variables to viewState.
  • Page_Unload, occurs after the page has executed
    and all other control events raised, a place for
    closing any temporary connections.

11
Configuration Stage
  1. If on a postback (not on an initial load) then
    the page and control viewstates are restored.
  2. Then the Page_Load event is fired.

12
Event Handling Stage
  • In this stage, we detect the event that initiated
    the round trip and process the associated code
    for this and other possible events.
  • While there is only one event that causes a round
    trip, there may be others that have occurred on
    the client that havent been processed yet. These
    are processed first but in no particular order.
  • The code associated with the control that
    initiated the round trip is always processed
    last.

13
Rending Stage
  • The rendering stage is where the HTML is
    generated to be sent to the browser.
  • The following items are sent to the browser
  • The static html
  • The results of any response.write methods
  • Output from all of the server controls on the
    page
  • The results of any Prerender code
  • Any in-line script code is run at the same time.
    (But no event processing.)

14
Cleanup Stage
  • The final stage of page processing occurs after
    the page has already been sent to the user.
  • Page_unload event occurs a good place to
    release resources like
  • Closing database connections
  • Close files
  • Discard any objects in the page
  • (.Net garbage collection will take care of these
    things so not typically used.)

15
Page_PreRender() Event
  • This is the last event happens before the page is
    posted from the server to the client, after all
    processing taken place. It is a convenient place
    to put away all viewState values, because you
    know the process for this round trip is over and
    no values are going to change until next trip.
  • Sub Page_Prerender()
  • viewState("counter") intCounter
  • viewState("topicsHashTable") htblTopic
  • End Sub
Write a Comment
User Comments (0)
About PowerShow.com