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. The 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.
Now, add the following namespaces in your code-behind.
C# code:
using System;
After that write the following code in button click
protected void Page_Load(object sender, EventArgs e) {}
// Set Session values during button click
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
' Set Session values during button click
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 the 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.
Now, add the following namespaces in your code-behind.
C# Code
using System;
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 on the previous page. We're also displaying the first name and the last name read from the session variables in a label.
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
Now, add the following namespaces in your code-behind.
C# code:
VB.NET Code
Now, add the following namespaces in your code-behind.
C# Code
using System;
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 on the previous page. We're also displaying the first name and the last name read from the session variables in a label.
VB.NET Code