What is a Session State in ASP.NET?
Session State is utilized to store the values of a client's browser session. A browser session is the duration of a user's website visit. Once the browser is closed, the session is finished. If the same user comes back to the website, a new session will be created. Session enables the data to be put away in one page and accessed on another page and supports any kind of item. In numerous sites, we can see its usefulness. For example - Once we are logged into a site, the site demonstrates our username (logged in user info) on every page. This happens because the site stores the username in a session.
When a client visits a web page, a new session is created with a unique session ID. The session ID is deleted once the user closes the current browser instance.
Let's test this.
Create an ASP.NET website using Visual Studio.
Open Default.aspx page and replace the current page content with the following code.
Defaulf.aspx
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>Asp.net Session State Example in C#, VB.NET</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h3>SessionStateData.aspx</h3>
- <table>
- <tr>
- <td>FirstName:</td><td><asp:TextBox ID="txtfName" runat="server"/></td>
- </tr>
- <tr>
- <td>LastName:</td><td><asp:TextBox ID="txtlName" runat="server"/></td>
- </tr>
- <tr><td></td> <td> <asp:Button ID="btnSubmit" runat="server" Text="Set SessionState Data"OnClick="btnSubmit_Click" /></td></tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Now, add following namespaces in your code-behind.
- using System;
- After that write the following code in button click
- protected void Page_Load(object sender, EventArgs e) {}
-
- protected void btnSubmit_Click(object sender, EventArgs e) {
- Session["FirstName"] = txtfName.Text;
- Session["LastName"] = txtlName.Text;
- Response.Redirect("Default2.aspx");
- }
VB.NET Code
- Partial Class SessionStateExample
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
- End Sub
-
- Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
- Session("FirstName") = txtfName.Text
- Session("LastName") = txtlName.Text
- Response.Redirect("Default2.aspx")
- End Sub
- End Class
In the above code, what we're doing is storing first name and last name in two Session variables. Once this data is stored, it will be available to the application during that session on every page.
Now, right-click on your website in Visual Studio Solution Explorer > select Add new item > select Web Form > give name as Default2.aspx.
Open the Default2.aspx page and write the following code.
Default2.aspx
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h3>Default2.aspx</h3>
- <table>
- <tr>
- <td colspan="2">Welcome <b><asp:Label ID="lblString" runat="server"/></b></td>
- </tr>
- <tr>
- <td>Your FirstName: </td><td><b><asp:Label ID="lblfName" runat="server"/></b></td>
- </tr>
- <tr>
- <td>Your LastName </td><td><b><asp:Label ID="lbllName" runat="server"/></b></td>
- </tr>
- <tr><td></td><td> </td></tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Now, add the following namespaces in your code-behind.
C# Code
After that, write the following code on the button click event handler. As you can see from this code, we're reading back the session variables we stored data in the previous page. We're also displaying the first name and the last name read from the session variables in a label.
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- if (Session["FirstName"] == null && Session["LastName"] == null) {
- Session["FirstName"] = "Sarthak";
- Session["LastName"] = " Varshney";
- lblString.Text = "Welcome " + Session["FirstName"] + Session["LastName"];
- } else {
- lblString.Text = Session["FirstName"] + " " + Session["LastName"];
- lblfName.Text = Session["FirstName"].ToString();
- lbllName.Text = Session["LastName"].ToString();
- }
- }
- }
VB.NET Code
- Partial Class Default2
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
- If Not IsPostBack Then
- If Session("FirstName") Is Nothing AndAlso Session("LastName") Is Nothing Then
- Session("FirstName") = "Sarthak"
- Session("LastName") = " Varshney"
- lblString.Text = "Welcome " & Convert.ToString(Session("FirstName")) & Convert.ToString(Session("LastName"))
- Else
- lblString.Text = Convert.ToString(Session("FirstName")) & " " & Convert.ToString(Session("LastName"))
- lblfName.Text = Session("FirstName").ToString()
- lbllName.Text = Session("LastName").ToString()
- End If
- End If
- End Sub
- End Class