Speed up development with full-stack environments for every branch.

Learn More

ASP.net How to use Session

9550 Runs 80796 Views 23393 Copies
Saved

Saved

invincibleideas 20

invincibleideas
published 4 years ago

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnCreateSession_Click(object sender, EventArgs e)
    {
        Session["Username"] = txtUserName.Text.Trim();
    }

    protected void btnRetrieveSession_Click(object sender, EventArgs e)
    {
        DisplaySessionValue();
    }

    protected void btnRemoveSession_Click(object sender, EventArgs e)
    {
        Session.Remove("Username");

        DisplaySessionValue();
    }

    protected void btnRemoveAll_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();

        DisplaySessionValue();
    }

    private void DisplaySessionValue()
    {
        if (Session["Username"] != null)
            lblSessionValue.Text = Convert.ToString(Session["Username"]);
        else
            lblSessionValue.Text = "No Value has been stored in session";
    }
} 
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).