Example of Session management

Example of Session management:

Session Management or working With Session in asp.net , Here we consider an Asp.net page with C#,In this Example we create a session on button click event and delete session on click event by Programming,
Syntax Of creating Session
Declare Session:
                          Session["mySession"] =null;
 Delete Session:

                          Session.Abandon();

Code For Aspx page in asp.net Programming:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Example of Session</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <asp:Label ID="session" runat="server"></asp:Label><br />

    <asp:Button ID="ok" runat="server" Text="Create Session" onclick="ok_Click" />
   
    </div>
    </form>
</body>
</html>
Create Session

Code of C# in asp.net 

using System;

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Session["mySession"] =null;
            //declear a null Session object name is mySession.
        }
    }
    protected void ok_Click(object sender, EventArgs e)
    {
        //first we ckeck Session........
        if (Session["mySession"] == null)
        {
            Session["mySession"] = "hello your Session is created";
            session.Text = Convert.ToString(Session["mySession"]);
            ok.Text = "delete Session";

            //session is id of Label control and ok is id of Button control.
        }
        else
        {
            if (Session["mySession"] != null)
            {
                Session.Abandon();
                session.Text = "";
                ok.Text = "create Session";
            }
        }
    }
}

Session Created by click on button

Other Post:


Reference :

Popular posts from this blog