Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

ASP.NET Reference Session:


In ASP.NET, "Session" refers to a mechanism that allows you to store user-specific data across multiple requests during a user's visit to your website. It's often used to maintain stateful information between different pages or interactions with the user.

Here's a simple explanation with an example:

Example

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Check if a session variable named "UserName" exists
If Session("UserName") IsNot Nothing Then
' If it exists, retrieve the username and display a personalized greeting
Dim userName As String = Session("UserName").ToString()
Response.Write("Welcome back, " & userName & "!")
Else
' If the session variable doesn't exist, prompt the user to log in
Response.Write("Please log in.")
End If
End Sub

Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
' Assuming txtUserName is a TextBox control where the user enters their username
Dim userName As String = txtUserName.Text

' Store the username in a session variable named "UserName"
Session("UserName") = userName

' Redirect the user to a welcome page
Response.Redirect("Welcome.aspx")
End Sub

In this example:

Session variables can be used to store any user-specific data that needs to persist across multiple requests. However, it's important to use session variables judiciously, as storing large amounts of data in sessions can consume server memory and impact performance.


Advertisement
Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java