Click here to Skip to main content
13,191,570 members (62,641 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 5 Oct 2006

State Management

, 5 Oct 2006
Rate this:
Please Sign up or sign in to vote.
Web form pages are HTTP-Based, they are stateless, which means they don’t know whether the requests are all from the same client, and pages are destroyed and recreated with each round trip to the server, therefore information will be lost, therefore state management is really an issue .
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

State Management in ASP.NET

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>

Web form pages are HTTP-Based, they are stateless, which means they don’t know whether the requests are all from the same client, and pages are destroyed and recreated with each round trip to the server, therefore information will be lost, therefore state management is really an issue in developing web applications

We could easily solve these problems in ASP with cookie, query string, application, session and so on. Now in ASP.NET, we still can use these functions, but they are richer and more powerful, so let’s dive into it.

Mainly there are two different ways to manage web page’s state: Client-side and Server-side.

1.Client-side state management : <o:p>

There is no information maintained on the server between round trips. Information will be stored in the page or on the client’s computer.

A. Cookies.

A cookie is a small amount of data stored either in a text file on the client's file system or in-memory in the client browser session. Cookies are mainly used for tracking data settings. Let’s take an example: say we want to customize a welcome web page, when the user request the default web page, the application first to detect if the user has logined before, we can retrieve the user informatin from cookies:
if (Request.Cookies[“username”]!=null)
lbMessage.text=”Dear “+Request.Cookies[“username”].Value+”, Welcome shopping here!”;
lbMessage.text=”Welcome shopping here!”;

If you want to store client’s information, you can use the following code:

So next time when the user request the web page, you can easily recongnize the user again.<o:p>

B. Hidden Field <o:p>

A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP Form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you would like to store directly in the page. Hidden field stores a single variable in its value property and must be explicitly added it to the page.
ASP.NET provides the HtmlInputHidden control that offers hidden field functionality.
protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
//to assign a value to Hidden field
Hidden1.Value=”this is a test”;
//to retrieve a value
string str=Hidden1.Value;

Note: Keep in mind, in order to use hidden field, you have to use HTTP-Post method to post web page. Although its name is ‘Hidden’, its value is not hidden, you can see its value through ‘view source’ function.<o:p>

C. <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:place w:st="on"><st1:PlaceName w:st="on">View <st1:PlaceType w:st="on">State <o:p>

Each control on a Web Forms page, including the page itself, has a ViewState property, it is a built-in struture for automatic retention of page and control state, which means you don’t need to do anything about getting back the data of controls after posting page to the server.

Here, which is useful to us is the ViewState property, we can use it to save information between round trips to the server.
//to save information
//to retrieve information
string shapes=ViewState[“shape”];

Note: Unlike Hidden Field, the values in ViewState are invisible when ‘view source’, they are compressed and encoded.


D. Query Strings <o:p>

Query strings provide a simple but limited way of maintaining some state information.You can easily pass information from one page to another, But most browsers and client devices impose a 255-character limit on the length of the URL. In addition, the query values are exposed to the Internet via the URL so in some cases security may be an issue.
A URL with query strings may look like this:

When list.aspx is being requested, the category and product information can be obtained by using the following codes:
string categoryid, productid;

Note: you can only use HTTP-Get method to post the web page, or you will never get the value from query strings.

2. Server-side state management: <o:p>

Information will be stored on the server, it has higher security but it can use more web server resources.<o:p>

A. Aplication object <o:p>

The Application object provides a mechanism for storing data that is accessible to all code running within the Web application, The ideal data to insert into application state variables is data that is shared by multiple sessions and does not change often.. And just because it is visible to the entire application, you need to used Lock and UnLock pair to avoid having conflit value.<o:p>





B. Session object <o:p>

Session object can be used for storing session-specific information that needs to be maintained between server round trips and between requests for pages. Session object is per-client basis, which means different clients generate different session object.The ideal data to store in session-state variables is short-lived, sensitive data that is specific to an individual session.

Each active ASP.NET session is identified and tracked using a 120-bit SessionID string containing URL-legal ASCII characters. SessionID values are generated using an algorithm that guarantees uniqueness so that sessions do not collide, and SessionID’s randomness makes it harder to guess the session ID of an existing session.
SessionIDs are communicated across client-server requests either by an HTTP cookie or a modified URL, depending on how you set the application's configuration settings. So how to set the session setting in application configuration? Ok, let’s go further to look at it.

Every web application must have a configuration file named web.config, it is a XML-Based file, there is a section name ‘sessionState’, the following is an example:

<sessionState mode="InProc" stateConnectionString="tcpip=" sqlConnectionString="data source=;user id=sa;password=" cookieless="false" timeout="20" />

‘cookieless’ option can be ‘true’ or ‘false’. When it is ‘false’(default value), ASP.NET will use HTTP cookie to identify users. When it is ‘true’, ASP.NET will randomly generate a unique number and put it just right ahead of the requested file, this number is used to identify users, you can see it on the address bar of IE:


Ok, it is further enough, let is go back to session object.
//to store information
//to retrieve information

C. Database <o:p>

Database enables you to store large amount of information pertaining to state in your Web application. Sometimes users continually query the database by using the unique ID, you can save it in the database for use across multiple request for the pages in your site.



ASP.NET has more functions and utilities than ASP to enable you to manage page state more efficient and effective. Choosing among the options will depand upon your application, you have to think about the following before making any choose:<o:p>

  • How much information do you need to store? <o:p>
  • Does the client accept persistent or in-memory cookies? <o:p>
  • Do you want to store the information on the client or server? <o:p>
  • Is the information sensitive? <o:p>
  • What kind of performance experience are you expecting from your pages? <o:p>

Client-side state management summary <o:p>

Method <o:p>

Use when<o:p>


You need to store small amounts of information on the client and security is not an issue.<o:p>

View state<o:p>

You need to store small amounts of information for a page that will post back to itself. Use of the ViewState property does supply semi-secure functionality.<o:p>

Hidden fields<o:p>

You need to store small amounts of information for a page that will post back to itself or another page, and security is not an issue. <o:p>

Note   You can use a hidden field only on pages that are submitted to the server. <o:p>

Query string<o:p>

You are transferring small amounts of information from one page to another and security is not an issue. <o:p>

Note   You can use query strings only if you are requesting the same page, or another page via a link. <o:p>

Server-side state management summary <o:p>


Use when<o:p>

Application state object <o:p>

You are storing infrequently changed, application-scope information that is used by many users, and security is not an issue. Do not store large quantities of information in an application state object.<o:p>

Session state object <o:p>

You are storing short-lived information that is specific to an individual session, and security is an issue. Do not store large quantities of information in a session state object. Be aware that a session state object will be created and maintained for the lifetime of every session in your application. In applications hosting many users, this can occupy significant server resources and affect scalability.<o:p>

Database support<o:p>

You are storing large amounts of information, managing transactions, or the information must survive application and session restarts. Data mining is a concern, and security is an issue.<o:p>

Developing ASP.NET programme is really funny, once you jump into it, you can feel the power of it. Next time let us talk about another topic: Cache. Enjoy .Net!!<o:p>







This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171017.1 | Last Updated 5 Oct 2006
Article Copyright 2006 by Member 3378211
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid