Introduction
Here I will explain SessionState in Asp.Net with example using c#.
We can check this one with simple example for that create one new website and open Default.aspx page and write the following code
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2" style="text-align:center;"><h3>Session State Data</h3></td>
</tr>
<tr>
<td>Name :</td>
<td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Email ID :</td>
<td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Contact No :</td>
<td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<asp:Button ID="btnsend" runat="server" Text="Send" OnClick="btnsend_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Screen Shot
C# Code
Default2.aspx
Output
Here I will explain SessionState in Asp.Net with example using c#.
What is SessionState?
The session state is used to maintain the session of each user throughout the application. Session allows information to be stored in one page and access in another page and support any type of object. In many websites we will see the functionality like once if we login into website they will show username in all the pages for that they will store username in session and they will access that session username in all the pages.
Whenever user enters into website new session id will generate for that user. This session Id will delete when he leave from that application. If he enters again he will get new session Id.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2" style="text-align:center;"><h3>Session State Data</h3></td>
</tr>
<tr>
<td>Name :</td>
<td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Email ID :</td>
<td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Contact No :</td>
<td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<asp:Button ID="btnsend" runat="server" Text="Send" OnClick="btnsend_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsend_Click(object sender, EventArgs e)
{
Session["name"] = TextBox1.Text;
Session["email"] = TextBox2.Text;
Session["contactno"] = TextBox3.Text;
Response.Redirect("Default2.aspx");
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsend_Click(object sender, EventArgs e)
{
Session["name"] = TextBox1.Text;
Session["email"] = TextBox2.Text;
Session["contactno"] = TextBox3.Text;
Response.Redirect("Default2.aspx");
}
}
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div><h1>
Default2.aspx Page</h1><br /><br />
Name : <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
Email ID :<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
Contact No :<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div><h1>
Default2.aspx Page</h1><br /><br />
Name : <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
Email ID :<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
Contact No :<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["name"].ToString();
Label2.Text = Session["email"].ToString();
Label3.Text = Session["contactno"].ToString();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["name"].ToString();
Label2.Text = Session["email"].ToString();
Label3.Text = Session["contactno"].ToString();
}
}
Output
Thank You...
No comments :
Post a Comment