Friday, September 30, 2011

ViewState And Session example


View state is used for store data at page level,you cant access View state data to another page.
Session state is used for store data and you can access anywhere in your website.
Session state is state specific information per client basis.

Example is given below.

View State:
protected void btnSubmit_Click(object sender, EventArgs e)
{
ViewState["DataScorceName"] = datasource;
ViewState["ClientName"] = txtCustomer.Text;
}

In same page use like this
txtCname.Text = ViewState["ClientName"]

Session State:

Session["EmpName"] = txtUser.Text;
retrieve session value in 2nd page
lblEmp.Text = Session["UserName"].ToString();