INT422 Internet III Web Programming on Windows - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

INT422 Internet III Web Programming on Windows

Description:

Learn about the concepts and classes that allow you to preserve data while users ... Note that in that pervious example 'SelectedIndex' will be shared between all ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 34
Provided by: team61
Category:

less

Transcript and Presenter's Notes

Title: INT422 Internet III Web Programming on Windows


1
INT422 Internet III Web Programming on
Windows
  • State Management

2
Learning objectives
  • State Management
  • Learn about the concepts and classes that allow
    you to preserve data while users are running your
    ASP.NET web applications

3
Why state management
  • ASP.NET web apps run on servers and serve clients
    using the HTTP communications protocol
  • HTTP is stateless
  • According to the HTTP protocol standard, the
    server follows a request reply processing
    model
  • After receiving a request, the server processes
    the request, and sends the reply to the browser
  • Then, it immediately discards all data associated
    with the request and reply

4
Why state management
  • Interactive web apps require data to be preserved
    between postbacks
  • Therefore, if this need cannot be met from within
    the HTTP protocol standard, the web server must
    provide facilities to meet this need
  • The ASP.NET web app environment provides a rich
    set of options for preserving data, and well
    explore many of them on the following pages

5
Web forms page processing 101
  • Important Understand the division of labor
    that happens during web form page processing
  • Browser user requests a web forms page via URL
  • Server sends this new page to the browser
  • Browser presents the user with a form
  • User interacts with the form, and posts it back
  • Server processes the form, and returns it to the
    browser
  • This is referred to as a round trip or
    postback

6
Round trips postbacks
  • Some user actions (events more on this soon)
    result in a round trip / postback
  • Command button clicks
  • Other controls that have the Autopostback
    property set to True (by default, this value is
    set to False)
  • Typical events that are supported
  • Click (button)
  • SelectedIndexChanged (dropdown list, listbox)
  • CheckChanged (radio button, check box)
  • TextChanged (textbox)

7
What ASP.NET does for you
  • It automatically saves page and control
    properties between round trips / postbacks
  • It provides you with the ability to save your own
    variable and object information between round
    trips / postbacks
  • It can detect when a web form is being requested
    by a user for the first time during an
    interactive session ( Page.IsPostBack )

8
Concept revisited events
  • Review / reminder
  • ASP.NET is an object oriented programming
    environment for creating and executing web apps
  • Web apps follow an event driven processing model
  • New A web form is processed as follows
  • Page_Init page initialization, control values
    restored
  • Page_Load runs user code in this code block
  • Validation if present, validation is done
  • Event handling executes the code block of the
    default event that caused the postback (e.g. a
    button click)
  • Page is rendered, unloaded, resources freed up

9
Event handler
  • Look at the declaration of any event handler code
    block for example, a button click
  • private void Button1_Click (object sender,
    System.EventArgs e)
  • // Your code goes here
  • private void InitializeComponent()
  • this.Button1.Click new System.EventHandler(thi
    s.Button1_Click)
  • this.Load new System.EventHandler(this.Page_Lo
    ad)

10
OK
  • back to preserving data

11
Where can data be preserved?
  • In a client server environment, there are
    (obviously) two choices
  • Client
  • Server
  • In your real-world web apps, data is often
    preserved in both places
  • OK thats great but where exactly does this
    data get stored?

12
State management options
  • CLIENT based options
  • On the page
  • View state property
  • Hidden form fields
  • Cookie
  • Query string
  • SERVER based options
  • Session state object
  • Application state object
  • Advanced in a database (often used with a
    cookie)

13
CLIENT view state
  • Built-in, automatic
  • Current state of the page and controls is encoded
    into a string and saved as an HTML hidden field

14
CLIENT view state
  • Values must be strings
  • Therefore, if a conversion is needed, you must do
    it
  • To create a new view state element
  • ViewState"color "yellow
  • To retrieve from a view state element
  • string s (string)ViewState"color

15
CLIENT view state tips
  • View state is enabled by default
  • If you arent going to use it, turn it off
  • You can also disable view state for a control
  • The property is EnableViewState
  • Why would you do this?
  • If a control contains lots of data, and the data
    source DOES NOT come from the view state
  • We may see this soon as we discuss DataSet
    objects
  • Incidentally you can disable view state for a
    page

16
CLIENT cookie
  • A cookie is a small amount of data stored either
    in a text file or in browser memory
  • Cookies
  • Can be temporary or persistent
  • Can store info about a client, a session, or an
    application
  • Are sent by the browser to the server along with
    a request (or postback)

17
CLIENT query string
  • A query string is data appended to a page URL
  • Format reminder
  • The page URL followed by
  • ? A question mark followed by
  • attributevalue one or more, separated by
  • Used in GETs, not POSTs
  • Implication use in Hyperlink web controls
  • Data limited in length URL query string lt 255

18
SERVER session
  • Very powerful yet easy-to-use method of
    persisting data between round trips / postbacks
  • Session concepts
  • HTTP is stateless a server does not know if a
    series/sequence of requests is coming from a
    specific client
  • And therefore it does not know if the
    series/sequence of requests is related logically
    in any way
  • Solution ASP.NET provides support for sessions

19
SERVER session
  • To support sessions, the server has executable
    components and a storage area
  • An important executable component is a State
    Server, which creates session identifiers, and
    manages the session lifetime and status
  • The storage area is where data that needs to be
    persisted during the session is stored
  • The storage area can be located
  • In memory on the server the web app is running on
    (default)
  • In memory on a separate state server network
    server/computer
  • In a SQL Server database on a network
    computer/server

20
SERVER session
  • When a browser client requests a URL for the
    first time, a session ID value is automatically
    created at the server
  • SessionID is a 120-bit string
  • Generated to guarantee uniqueness and randomness
  • Sent to the client with the page using an HTTP
    cookie (usually) or a modified URL (depending on
    settings)
  • Stays with round trips/postbacks

21
SERVER session
  • Session items are binary streams
  • More powerful than simple strings, because you
    can store objects in session state collection
    objects
  • Therefore, you must manage type conversions in
    code

22
SERVER session example 1
  • To create a new session item string
  • string s "yellow
  • Session"color s
  • To retrieve from a session item string
  • string s String.Emptytry
  • s (string)Session"colorcatch

23
SERVER session example 2
  • To create a new session item object
  • //Assume "Student" is a class/type
  • Student s New Student()
  • //More code here to set properties
  • Session"thestudent s
  • To retrieve from a session item object
  • Student s New Student()
  • s (Student)Session"thestudent

24
SERVER session QA
  • When does a session start?
  • A browsers first request to a URL in a web app
  • How long does a session last?
  • By default, set for 20 minutes from the most
    recent request / round trip / postback
  • How does a session end?
  • Method 1 timeout
  • Method 2 user closes browser, or navigates away
    from the web app
  • Method 3 use Session.Abandon method (or other
    methods from other classes)

25
SERVER session error handling
  • Heres something to remember if a session times
    out, data stored in a session object will be
    discarded
  • Unless you handle this situation, your web app
    will throw an exception so, trap it
  • Try
  • myVar Convert.ToString(Session("color"))
  • //Other code here...
  • Catch
  • Server.Transfer("login.aspx")

26
SERVER application
  • An application-wide storage mechanism
  • Data available to all users of this application
  • Server-managed

27
CLIENT or SERVER advanced
  • For your information and self-study only
  • ASP.NET supports serialization of objects to a
    stream (e.g. XML)
  • This can be yet another way of persisting data
  • ASP.NET supports data caching
  • In the Application object
  • Good for expensive-to-create data

28
State management recommendations
  • See the MSDN Library article for more detail
  • Consider the following criteria
  • How much information do you need to store?
  • Does the client accept cookies?
  • Do you want to store info on client or server?
  • Is the information sensitive?
  • What performance criteria are there?

29
State management client side
  • Client side options features
  • Client-side storage this option does not use
    any server resources (memory or storage)
  • Minimal security
  • Simple
  • Good performance usually
  • Not good for vast amounts of information to be
    persisted, because it round-trips
  • Some users turn off browser cookies, so you
    cannot count on using some client-side options

30
State management server side
  • Server side options features
  • Easy to use in the ASP.NET environment
  • Durability and reliability
  • Scalability (across server farms etc.)
  • Can work with browsers that have turned off
    cookies
  • Theres a performance implication if you store
    vast amounts of data in session state on a server
    that has hundreds or thousands of other similar
    web apps guess what performance suffers
  • Security is good

31
Encapsulating ViewState and Session
  • To hide the ViewState use, and make it look like
    an attribute of a class you could do the
    following
  • public int SelectedIndex
  • get
  • object obj ViewState"SelectedIndex"
  • if(obj null)
  • return -1
  • else
  • return (int)obj
  • set
  • ViewState"SelectedIndex" value
  • Remember that the scope of this property (because
    of the ViewState) is the page.

32
Encapsulating ViewState and Session
  • If you need to carry the value of the property
    from one page to another, you must use Session.
  • public int SelectedIndex
  • get
  • try
  • return (int)Session"SelectedIndex"
  • catch
  • return -1
  • set
  • Session"SelectedIndex" value
  • Remember that this property stays alive while the
    browser is open and is not inactive for more than
    20 minutes.

33
The uniqueness of Session variables
  • Note that in that pervious example
    SelectedIndex will be shared between all
    instances of its owner, which means
  • If Menu X, Y And X.SelectedIndex 20
  • Then the value of Y.SelectedIndex will be 20
    too, since they both refer to the same
    SessionSelectedIndex. To fix this you can
    concatenate the programmatic identifier of the
    owner to the session name
  • public int SelectedIndex
  • get
  • try
  • return (int)Session"SelectedIndex
    this.ID
  • catch
  • return -1
  • set
  • Session"SelectedIndex this.ID
    value
Write a Comment
User Comments (0)
About PowerShow.com