Session overview:
Session object is use to maintain the session state onto the server. Server creates unique session id for each browser. It executes on server and also resides on server. So the data stored into session will be secure. We can store object type (any type, any amount) data in the session. Default time for a session is 20 minutes but it can be change using the session state tag time out property in web config file.
For example-Suppose we want to set the time out property equals to forty then session will expire after forty minutes. Session tag use as follows:
<session state timeout=40/>.
Session variables are declared on fly that is no need for prior declaration. Session variable are used to maintain the session. By default session state managed in the IIS server known as inproc mode. Inproc sessions are very fast due to the existence in the same process domain. But we can also create the out process session having three possible modes. These are as follows:
- Custom.
- State secure.
- Sql server.
We can also create the cookie less session. In which the session id is send back to the client with the help of the URL that is session id is pervaded into the URL Values are store in the session on bases of key.
Note: In .NET 2.0 by default session state is not manage by the server until we do not store any data in session state.
To manage the session:
We can see how the server manages the session id as follows:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Session.SessionID);
}
}
This code is representing new session id at every time on button click or click on refresh the page and that will different each and every time
What is Postback property?
Postback is used to send back the web form on the web server with the data in its control for further processing. After processing on the server the page will be again loading into the memory. This whole process is known as the round tripping of the page.
For example:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["xx"] = "MCN";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Session.SessionID);
}
}
This code is generating the unique session id. Here post back is send back the web form on the server for further processing. By default ispotstback property is false so is not postback is true.
Methods for removing item from session:
- Session.Remove("xx");//delete an item from session state collection
- Session.RemoveAll();//remove all keys and values from the session state collection.
- Session.RemoveAt(); //delete an item at specified index from the session state collection
Global.aspx:
For trapping the session we have .aspx file. global.aspx is used to trap the globaly event of the application. It contain eight methods used to trp the session.these are as follows:
- Application_start()
- Application_end()
- Session_start()
- Session_end()
- Application_begin request()
- Application_end request()
- Application_authentication request()
- Application_error()
Create session:
Webform1.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="0" cellspacing="0" border="2" width="60%" height="200px"><tr><td align="center" valign="top" style="padding-top:20px;">
<asp:Label ID="lblname" runat="server" Text="Enter Name">
</asp:Label>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<br /><br />
<asp:Label ID="lbladress" runat="server" Text="Enter Address"></asp:Label>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnsubmit" runat="server" Text="Submit" OnClick="btnsubmit_Click"/>
</td></tr></table>
</div>
</form>
</body>
</html>

Webform1.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void btnsubmit_Click(object sender, EventArgs e)
{
Session["xx"] = txtname.Text;
Session["yy"]=txtaddress.Text;
Response.Redirect("Webform2.aspx");
}
}
Webform2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Webform2.aspx.cs" Inherits="Webform2" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div><table cellpadding="0" cellspacing="0" border="2" width="60%" height="200px"><tr><td align="center" valign="top" style="padding-top:20px;">
<asp:Label ID="lblname" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtaccessname" runat="server"></asp:TextBox>
<br /><br></br>
<asp:Label ID="address" runat="server" Text="Addres"></asp:Label>
<asp:TextBox ID="txtaccessaddress" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnaccessvalue" runat="server" Text="Click to Access value" OnClick="btnaccessvalue_Click" />
</td></tr></table>
</div>
</form>
</body>
</html>

Webform2.aspx.cs:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Webform2 : System.Web.UI.Page
{
protected void btnaccessvalue_Click(object sender, EventArgs e)
{
txtaccessname.Text = Session["xx"].ToString();
txtaccessaddress.Text = Session["yy"].ToString();
}
}
This program is create for storing the name and address of the person in session.webform one is used for submit the information and another is used for access the information.we can store the large amount of data in the session.