Follow us

Header Ads

Session in Asp.Net WebForm

 Session1.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            height: 26px;
        }
        .auto-style3 {
            height: 26px;
            width: 81px;
        }
        .auto-style4 {
            width: 81px;
        }
    </style>
</head>
<body>
    
    <form id="form1" runat="server">
        <div>
            <table class="auto-style1">
                <tr>
                    <td class="auto-style3">UserName</td>
                    <td class="auto-style2">
                        <asp:TextBox ID="USERTextBox" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style4">&nbsp;</td>
                    <td>
                        <asp:Button ID="SUBMITButton" runat="server" OnClick="SUBMITButton_Click" Text="Submit" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

 Session1.aspx.cs

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

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

    }

    protected void SUBMITButton_Click(object sender, EventArgs e)
    {
        Session["user"] = USERTextBox.Text;
        Response.Redirect("Session2.aspx");
    }
}

 Session2.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Session3.aspx">GoToSession3</asp:LinkButton>
        </div>
    </form>
</body>
</html>

 Session2.aspx.cs

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

public partial class Session2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     if(Session["user"]!= null)
        {
            Response.Write("Welcome" + Session["user"].ToString());
        }
        else
        {
            Response.Redirect("Session1.aspx");
        }
    }
}

 Session3.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="LOGOUT" />
        </div>
    </form>
</body>
</html>

 Session3.aspx.cs

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

public partial class Session3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] != null)
        {
            Response.Write("Welcome" + Session["user"].ToString());
        }
        else
        {
            Response.Redirect("Session1.aspx");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["user"] = null;
        Response.Redirect("Session1.aspx");
    }
}

Post a Comment

0 Comments