Overview On State Management In ASP.NET

State Management

In ASP.NET, the webserver do not have an idea about the request coming from new user or old user. The server is unable to identify that the user is returned user or a new user. To solve these problems we use state management.

State management is of the following two types:

  1. Client side state management
  2. Server side state management

Client side state management

Client side state management handled by:

  • View state
  • Hidden fields
  • Cookies
  • Query string

Server side state management

Server side state management handled by:
  • Session state
  • Application state

View state

  • ASP.NET technology provides view state/control state feature to the web forms.
  • View state will remember the controls when page is post back to server.
  • View state stored at client side in hidden fields and encrypted.
  • We can enable and disable the feature in webpages by set view state property to true (or) false.

Example for view state

Creating view state

View state.add(“vikram”);
//reuse view state
String name=view state(“vikram”).tostring();


Hidden fields

  • Hidden field id used for storing some data on the webpage.
  • In the html input type we can specify type as hidden.
  • ASP.NET provides HtmlInputControl that offers hidden field functionality.
  • Don’t store security information in hidden fields because values are not encrypted.

    // declaring hidden field variable

    Protected Htmlhiddeninput name;

    // populating hidden field variable

    Name.value=“vikram”;

    // reuse hidden variable

    String sname=name.value;

Cookies

  • Cookie is a small amount of data stored at client system posted by the server.
  • Cookies are generally used for tracking user requests.

We have two types of cookies

  1. Short time cookies.
  2. Long time cookies.
  • Short time cookies stored in the web browser.
  • Long time cookies stored in file explorer.
  • We can manage cookies on client system.
  • Cookies can store up to 10kb.

// Storing value in cookie
HttpCookie cookie = new HttpCookie("NickName");
cookie.Value = “vikram";
Request.Cookies.Add(cookie);
// Retrieving value in cookie
lblNickName.Text = "Welcome" + Request.Cookies["NickName"].ToString();

Query string

  • Query string is the limited way to pass information to the web server while navigating from one page to another page.
  • This information is passed in url.
  • We need to use HTTP Get method to post query strings to server.
  • Query string limit is up to 250 characters.

Example for query string

http://localhost:49262/welcome.aspx?Name=vikram&LastName=12345

  • In the above url, the name and last name values transfer from one page to another. We can use those details in destination page.

Page 1

  1. protected void Button1_Click(object sender, EventArgs e)   
  2. {  
  3.     Response.Redirect("welcome.aspx?Name=" + this.TextBox1.Text + "&LastName=" + this.TextBox2.Text);  
  4. }
Page 2
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {  
  3.     string s = Request.QueryString["name"];  
  4.     Label1.Text = “welcome“ + s;  
  5. }
Application state
  • Application State is used to store information which is shared among users of the ASP. NET web application.
  • Once we created application state we can access in any page.

Application state details will clear when the process is restarted.

//storing information into application state
Application["Name"] = “vikram";
//retrieving details from application state
Label.text=Application[“name”].tostring( );


Session state

  • Session sate is used to store and retrieve information.
  • We can use the session information in every page in current project.

//Storing informaton in session state
Session[“name"] = “vikram";
//Retrieving information from session state
string str = Session["NickName"];
Label1.text=str;

Up Next
    Ebook Download
    View all
    Learn
    View all