State Management Technique in .NET

State Management Technique is a process by which we maintain the state and page into multiple requests for the same or different page.

  • Cookies
  • Query String
  • View State
  • Session
  • Application object 

1. Cookies in ASP.NET

There are two types of cookies in ASP.NET.

  1. Session Cookie/Non persistence Cookie
  2. Persistence Cookie

Persistent cookies are the same as session cookies except that persistent cookies have an expiration date.

On the Web Form 1

protected void btnclick_click (object sender,Event args e)
{
     HttpCookie M1 = new Httpcookie["userinfo"];
     M1["name"] = textbox1.text;
     M1["email"] = textbox2.text; // if it is persistence cookie then here we will write "CookieObject.Expires = DateTime.Now.AddDays(10);"
     Responce.cookies.Add("M1");  
     Responce.redirect("webform2.aspx");
}

Cookies

On the Web Form 2

protected void Page_Load (object sender,Event args e)
{
    HttpCookie M1 = Request.cookies["userinfo"];
    if(Cookie ! = null)
    lable1.text = M1["name"];
    lable2.text = M1["email"];
}

Non persistence Cookie

2. Query String Management System

On WebForm 1

protected void btnclick_click (object sender,Event args e)
{
    Responce.Redirect ( "webform2.aspx?username+" + textbox1.text + "& useremail + " textbox2.text);
}

On WebForm 2

protected void Page_Load (object sender,Event args e)
{
    lable1.text = Request.querystring [ "ussername"];
    lable2.text = Request.querystring [ "usseremail"];
}

3. Session State Management System

On WebForm 1

protected void btnclick_click (object sender,Event args e)
{
    Session [ "username"] = textbox1.text;
    Session [ "useremail"] = textbox2.text;
}

On WebForm 2

protected void Page_Load (object sender,Event args e)
{
    if ( Session["username"] ! = null)
    {
        lable1.text = Session [ "username"].ToString;
    }
    if ( Session["useremail"] ! = null)
    {
        lable2.text = Session [ "useremail"].ToString;
    }
} 

4. Application State Management System

On WebForm 1

protected void btnclick_click (object sender,Event args e)
{
    Application [ "username"] = textbox1.text;
    Application [ "useremail"] = textbox2.text;
} 

On WebForm 2

protected void Page_Load (object sender,Event args e)
{
    lable1.text = Application [ "username"].ToString();
    lable2.text = Application [ "useremail"].ToString();
}

5. View State/Hidden Field Management System

It is used to maintain the state of controls during page post-back and if we save any Control value or anything in a view state then we can access the value throughout the page whenever it is needed.

The server controls are loaded and initialized from the Web form’s view state. This is the first step in a Web form’s life cycle.

Coding For View State

protected void page_load(object sender,Event args e)
{
    if (! ispostback)
    {
       string str = "welcome to Asp.net-munesh blog";
       if(view state["sample text"]==NULL )
       {
          view state["sample text"]==str;
       }
    }
}
protected void btnclick_click (object sender,Event args e)
{
    lblstring.text = viewstate["sample text"].to string();
}

You can also go for more details in my blog:

dotnet-munesh


Similar Articles