Search This Blog

22 June 2011

Session State

what is session:

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests. ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session.
Session state information is available to all pages opened by a user during single visit. Therefore, you can use session state to store user specific information. If different user are using your application,each user session has a different session state.


how to create session:


Session["name"]=txtname.Text;
//storing the value of textbox in session
Session["name"]="David";
//storing static value in session
or
Session.Add("name",txtname.Text)
Session.Add("name","David")

how to retrieve session value:

string str=Session["name"].ToString();
lbname.Text=Session["name"].ToString();


how
to destroy session's value :

An important consideration for using Session state is that the Session does expire. By default, if a user does not access their Session data within 20 minutes (by default), the Session will expire and all items that had been stored in the Session will be discarded. Because of this, it is important to check the object that is returned from the Session to see if it exists or if it is null before you try to work with it.

if(Session["name"]!=null)
{
lbname.Text=Session["name"].ToString();
}


The Session Timeout is adjustable through a web.config setting but increasing the timeout value can put memory pressure on your server that may be undesirable.



forcefully destroy the session value, other session methods are:
  • Session.Abandon() - removes the Session and all items that it contains
  • Session.Clear() - removes all items from the Session
  • Session.RemoveAll() - removes all items from the Session
  • Session.Remove("itemName") - removes the item that was stored under the name "itemName"



17 June 2011

AutoPostBack Property


Autopostback is the mechanism, by which the page w
ill be posted ,back to the server automatically based on some events in the web controls. In some of the web controls, the property called auto post back, which if set to true, will send the request to the server when an event happens in the control. If this property is set to TRUE the automatic post back is enabled, otherwise FALSE. Default value of AutoPostBack property is FALSE.

For Example



Why we need to set autopostback=true of controls

Consider a scenario where the web page is used for entering the user information. The page contains two dropdownlist controls ddlcountry and ddlstate. When user selects the country, the appropriate states be filled in the ddlstate which is loaded from the database. For achieving this requirement, we can set the autopostback property of ddlcountry to true. If we do that we can handle the event in the server side and write code to populate the ddlstate with the values from the database.
This is how we use the autopostback property.


By default the button,linkbutton and imagebutton has an autopostback as an event. The code behind that you write is to handle this postback. A button cannot have an explicit button postback but is in-built.

How to print the values of Asp controls

lbname.Text= txtname.Text

lbhobby.Text=ddlhobby.SelectedItem.Text //printing text field data

lbhobby.Text=ddlhobby.SelectedItem.Value //printing value field data

lbgender.Text=rdbmale.Text

lbcolor.Text=chkcolor.Text

lbphoto.Text=FileUpload1.Filename

lbid.Text=HiddenField1.Value