How to create Sessioin in ASP.NET -Example


Session.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="16_Session1.aspx.cs" Inherits="_16_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 {
            width: 168px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
        </table>
   
    </div>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="text-decoration: underline" Text="OK" />
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" Text="plz Try again" Visible="False"></asp:Label>
    </form>
</body>
</html>

Session.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 _16_Session1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == TextBox2.Text)
        {
            Session["user"] = TextBox1.Text;
            Response.Redirect("~/16_session2.aspx");
        }
        else
        {
            Label3.Visible = true;
        }
    }
}

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        Hi......Welcome
        <asp:Label ID="Label1" runat="server"></asp:Label>
   
    </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 _16_session2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["user"].ToString();
    }
}

Share this

Related Posts

Previous
Next Post »