The following example shows how to create session variables in an ASP.NET page for the first and last name of a user, and set them to values retrieved from TextBox controls.
- Sessions are used to store the data for the user just like cookies.
- The Session object stores information about, or change settings for a user session.
- Variables are stored in a Session object hold information about one single user.And are available to all pages in one application.
- Common information stored in session variables are name, id, and preferences.
- The server creates a new Session object for each new user, and destroys the Session object when the session expires.
Set values to session from Textbox controls:
- Session["UserName"] = UserNameTextBox.Text;
- Session["Password"] = PasswordTextBox.Text;
On ASPX Page:
- string loggedUsername = Session["UserName"] as string;
- mylabel.Text = "Currently Logged User is" + (loggedUsername != null) ? loggedUsername : "Unknown Person";
Thanks for learning my blogs !!!