timeout attribute session, application & viewstate

Differences between ViewStae, SessionState and ApplicationState
variables
Initially when page is loaded, its the get request
ViewState:
My Definition: variables available on the webfrom which is currently
being displayed
PPT notes
1) ViewState of webform is available only with in the webform
2) ViewState is stored on the page in a hiddenfield called _ViewState.
Because of this, the ViewState, will be lost, if you navigate away from
the page, or if the browser is closed.
3) ViewState is used by all asp.net controls to retain their state
across postback request
----------------------- Code for View
State---------------------------------------------------
public partial class ViewState : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/*Check if it is not a get request*/
if (!IsPostBack)
{
if (ViewState["Clicks"] == null)
{
ViewState["Clicks"] = 0;
}
TextBox1.Text = ViewState["Clicks"].ToString();
}

}

protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = ((int)ViewState["Clicks"] + 1).ToString();
ViewState["Clicks"] = Convert.ToInt32(TextBox1.Text);

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}
}
-------------------------- End of code for
ViewState------------------------------------------
Now let us create a webform ViewState2, with exactly the same code as
for ViewState,  and run the application on browser
first run this
localhost:port/ViewState.aspx - tab 1 and press the botton so that the
count in textbox is not 0
then in different tab run this:
localhost:port/ViewState2.aspx - tab 2
we can now see that the textbox of viewstate2.aspx has zero count and
there for is not related to viewstate1.aspx.
That is because viewstate variable of viewstate is not available for
viewstate2.aspx.
ViewState is saved is store on client or on the browserpage in a hidden
field called _ViewState which is encoded in base 64.
Since ViewState is stored on the page, if you close the page or navigate
away from the page, the viewstate will be lost.
viewstate value will not be lost if you refresh the same page
-----------------------------------------End of Explaination for view
state-------------------------------------

-----------------------------------------Begining of SessionState
----------------------------------------------
SessionState:
myDefinition: variables available unitll the end of the session, that is
untill the user doesn't close the browser
PPT notes
1) SessionState variables are available across all pages, but only for a
given single session. Session variables are like single-user global
data.
2) Session state variables are stored on the web server.
3) Session state variabels are cleared, when the user session times out.
The default is 20 minutes. This is configurable in web.config.
------------------------------------------Code for
SessionState-------------------------------------------------
public partial class SessionState1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Clicks"] == null)
{
Session["Clicks"] = 0;
}
TextBox1.Text = Session["Clicks"].ToString();
}

}

protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = ((int)Session["Clicks"] + 1).ToString();
Session["Clicks"] = Convert.ToInt32(TextBox1.Text);

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}
}
------------------------------------------End of code for Session
State-----------------------------------
NOTICE: the variable ViewState changes to Session and that makes a
variable from viewstate to session state
Now let us create a webform SessionState2, with exactly the same code as
for SessionState1 and ViewState,  and run the application on browser
first run this
localhost:port/SessionState1.aspx - tab 1 and press the botton so that
the count in textbox is not 0
then in different tab run this:
localhost:port/SessionState2.aspx - tab 2
we can now see that the textbox of viewstate2.aspx has same count and
therefore is same as for SessionState1.aspx.
That is because SessionState variable of SessionState1 is not available
for SessionState2.aspx because they are running for same user.
SessionState variable is shared across all the webform and all pages.
This is because of cookies, we can
disable cookies in  web.config file by going into <system.web> tag and
adding
<sessionState mode = "Inproc" cookieless = "true"></sessionState> // see
code explaination in 4th commit
when you make a session cookie less the urls appears as
localhost:port/(sessionid)/SessionState2.aspx and as long as the
sessionId is same in browser windows the session variables will retain
the
even if SessionState is different
localhost:port/(sessionid)/SessionState1.aspx
SessionState data is shared data across all pages for same user or for
specif user session.
SessionState variables are stored in server and as soon as the session
expires the variables for the sessions also get deleted
Each session has a default life of 20 minutes, this time can be changes
in web.config file by adding this
<sessionState mode = "InProc" cookieless = "true"
timeout="1"></sessionState>  this is under <system.web> tag we have set
the timeout as 1 min, so after one minute session will expire.
--------------------------------------End of Explaination for
SessionState--------------------------------
Application State:
my Definition : variabels that are available until the webapplication is
running and the server doesn't stop it
PPT notes:
1) Application State variables are available across all pages and across
all sessions. Application state variables are like multi-user global
data.
2) Application State variables are stored on the web server.
3) Application State Variables are cleared, when the process hosting the
application is restarted.
-------------------------------------Code for
ApplicationState----------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Application["Clicks"] == null)
{
Application["Clicks"] = 0;
}
TextBox1.Text = Application["Clicks"].ToString();
}

}

protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = ((int)Application["Clicks"] + 1).ToString();
Application["Clicks"] = Convert.ToInt32(TextBox1.Text);

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}
-----------------------------------End of code for
ApplicationState-----------------------------------------
At this point we have set cookieless="true" so we will see the and
timout=1 mins
so let us open up the browser and run localhost for the application by
typing in localhost/ApplicationState1.aspx
then the browser responds back to the get request with a session id and
the url appears as
localhost/sessionId1/ApplicationState1.aspx
increment the number in the textbox to 4 by clicking the button
now openup localhost/ApplicationState2 in the same browser but it
different tab ,
then the browser responds back to the get request for ApplicationState2
with the sessionId1 and url appears as
localhost/sessionId1/ApplicationState2.aspx. and the number in the
textbox remains same (provided you do this within the time specified for
timeout)
also when you open another browser and type localhost/application1.aspx,
and eventhough sessions in chrome and mozilla are not related the number
in textbox remains the same, which is 4.
Remember the textbox in for the applicationstate variables will remain
the same and it will never lose the value unless the server stops
working or is rebooted. This value will not depend on timeout or the
cookies
-------------------------------End of Explaination for
ApplicationState-------------------------------------