Wednesday, May 23, 2007

View state and Session State explained

All about viewstate:

Adding item to the viewstate:

Page_load(-)
{
if(page.ispostback==false)
{
ViewState.Add(“SomeValue”,”HelloWorld”);
}
}

retrieving viewstate item value:
protected void button_click(-)
{
resultLabel.Text=ViewState[“SomeValue”].ToString;

}

All about session:

You can't really use a cookie to store a shopping cart. A cookie is just too small and too simple. To enable you to work around the limitations of cookies, the ASP.NET Framework supports a feature called Session state. Like cookies, items stored in Session state are scoped to a particular user. You can use Session state to store user preferences or other user-specific data across multiple page requests.
You add items to Session state by using the Session object.
Adding an item in session state in global.asax
Void session_start(object sender,eventargs e)
{
Session.Add(“SomeValue”,”Session.SessionID”);
}
Retrieving session state item when postback:
Protected button_click (object sender,eventargs e)
{
resultLabel.Text=Session[“SomeValue”].ToString;
}

No comments: