State Management in ASP.NET

State management:

1)http is a stateless protocol

2)http behavior is go, get and forget

Go->the client request will be taken to webserver by establishing connection

Get->the response will be taken from webserver to client

Forget->the connection will be closed between browser and webserver

3)http as a stateless protocol can be considered as an advantage, it will reduce burden on webserver

4)the requirement is website should able to remember user information. The solution is statemanagement

5)website remembering users information is called “statemanagement”.

  Asp.net is providing different techniques to work with statemanagement

1)viewstate

2)cookies

3)session

4)application

5)cache

 

Viewstate:

1)viewstate can be used to maintain user information towards a webpage for different postback submissions

2)the user information can be controls data[state] (or) normal data[variables]

3)controls state will be maintained implicitly with the help of viewstate. Developer can handle normal data[variables] using viewstate object

 

Storing data into viewstate:

  Viewstate[“varname”]=value;

The value will be stored in the form of an object [variable will be declared with object datatype, this allows storing any type of data]

 

Reading data from viewstate:

Viewstate[“varname”]->it returns value in the form of an object, it can be type casted to required type

EX:

Viewstate[“varname”].Tostring()->it will convert an object type data into string type.

(datatype) viewstate[“varname”]->it will convert an object into specified datatype

 

 

Creating website to work with viewstate

 

Goto visual studio

Start->run->devenv

It will display main window of visual studio

File menu->new->website->visual c#->select asp.net empty website

Weblocation->e:\aspnet\viewstatesite[drive:\dir\websitename]

Visual studio create a folder with website name, in this folder website related files will be placed

 

Add webform

Goto view menu and select solution explorer

Right click on website path and select add new item

Select webform

Give name as login.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

    </div>

    </form>

</body>

</html>

 

 goto design part

 


User name                              

 


Password   

Login

 

 


                                 Label3

 

Textbox1 properties:

Id-t1

 

Textbox2 properties:

Id-t2

Textmode-password

 

Button1 properties:

Text-login

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class login : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (t1.Text == "peers" && t2.Text == "info") Server.Transfer("welcome.aspx");

        else

        {

            // invalid login parameters

            if (ViewState["n"] == null) ViewState["n"] = 1;

            else

                ViewState["n"] = (int)ViewState["n"] + 1;

            Label3.Text = "number of attempts failed:" + ViewState["n"].ToString();

        }

    }

}

 

Add webform

Goto view menu and select solution explorer

Right click on website path and select add new item

Select webform

Give name as welcome.aspx

 

Goto design part

Place a label with text welcome to mysite

 

Goto view menu and select solution explorer

Right click on login.aspx set as start page

 

Goto contrl F5

 

Note:

1)viewstate information will be maintained as part of webpage output with in hiddenfield

2)hiddenfield will maintain viewstate information [variable data] in a encrypted format

3)textbox without visibility is called “hidden field”