Tuesday, 18 July 2023

Session State Start on Response.Redirect event through user input in ASP.NET

Session State Start on Response.Redirect event through user input in ASP.NET

SessionState3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SessionState3.aspx.cs" Inherits="ASPNETALL.SessionState3" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="auto-style1">
            <tr>
                <td><h1>Session State Form3</h1></td>
            </tr>
            <tr>
                <td>User Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Uesr Email:
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnSendData" runat="server" Font-Bold="True" Text="SessionState4" OnClick="btnSendData_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

SessionState3.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASPNETALL
{
    public partial class SessionState3 : System.Web.UI.Page
    {
        protected void btnSendData_Click(object sender, EventArgs e)
        {
            Session["Name"] = txtName.Text;
            Session["Email"] = txtEmail.Text;
            Response.Redirect("~/SessionState4.aspx");
        }
    }
}

SessionState4.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SessionState4.aspx.cs" Inherits="ASPNETALL.SessionState" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="auto-style1">
            <tr>
                <td colspan="2"><h1>Session State Form4</h1></td>
            </tr>
            <tr>
                <td colspan="2">User Name :
                    <asp:Label ID="lblName" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>User Email :
                    <asp:Label ID="lblEmail" runat="server"></asp:Label>
                </td>
                <td>
                    <asp:Button ID="btnLogOut" runat="server" Font-Bold="True" ForeColor="Red" OnClick="btnLogOut_Click" Text="LogOut" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

SessionState4.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASPNETALL
{
    public partial class SessionState : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["Name"]!=null)
            {
                lblName.Text = Session["Name"].ToString();
            }
            if(Session["Email"]!=null)
            {
                lblEmail.Text = Session["Email"].ToString();
            }
            Response.Redirect("~/SessionState3.aspx");
        }
        protected void btnLogOut_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.Clear();
            Session.Remove("Name");
            Session.Remove("Email");
            Session.RemoveAll();
            Response.Redirect("~/SessionState3.aspx");
        }
    }
}
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos

0 comments

Post a Comment