How to use ASP.NET Session Object
Rate this article
0 out of 5
In this Video Tutorial we will show you how to use ASP.NET Session Object



Source Code

Admin.aspx

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

<!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>Untitled Page</title>
</head>
<body bgcolor="#ccccff">
    <form id="form1" runat="server">
    <div>
        <table style="height: 66px" width="950">
            <tr>
                <td align="center" style="width: 946px; height: 42px">
                    <asp:Label ID="Label1" runat="server" Font-Size="X-Large" ForeColor="#804000" Text="WELCOME ADMINISTRATOR"
                        Width="324px"></asp:Label></td>
            </tr>
            <tr>
                <td align="right" style="width: 946px">
                    <asp:Button ID="Button1" runat="server" Height="28px" OnClick="Button1_Click" Text="LogOut"
                        Width="94px" /></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

Admin.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] == null)
        {
            Response.Redirect("Login.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("LogOut.aspx");

    }
}

Login.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="
Log
in.as
px.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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="height: 79px" width="950">
            <tr>
                <td align="center" style="height: 77px">
                    <asp:Label ID="Label1" runat="server" Font-Names="Arial Black" Font-Size="X-Large"
                        Text="Login" Width="219px"></asp:Label>
                </td>
            </tr>
            <tr>
                <td align="center" style="height: 28px">
                    <asp:Label ID="LabelUserName" runat="server" Text="UserName" Width="150px"></asp:Label>
                    <asp:TextBox ID="TextBoxUsername" runat="server" Width="200px"></asp:TextBox></td>
            </tr>
            <tr>
                <td align="center" style="height: 26px">
                    <asp:Label ID="Label2" runat="server" Text="Password" Width="150px"></asp:Label>
                    <asp:TextBox ID="TextBoxPassword" runat="server" Width="200px" TextMode="Password"></asp:TextBox></td>
            </tr>
            <tr>
                <td align="center" style="height: 36px">
                    <asp:Label ID="Label3" runat="server" Width="289px"></asp:Label></td>
            </tr>
            <tr>
                <td align="center">
                    <asp:Button ID="Button1" runat="server" Text="LogIn" Width="129px" OnClick="Button1_Click" /></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

Login.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Check the password or UserName field is empty

        if (TextBoxUsername.Text == "" && TextBoxPassword.Text == "")
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('You are an UnAuthorized person')</script>");
            //LabelValid.Text="You are an UnAuthorized person";
        }
            //check the user is admin 
        else if (TextBoxUsername.Text == "Admin" && TextBoxPassword.Text == "Admin")
        {
            Session["name"] = TextBoxUsername.Text;
            Response.Redirect("Admin.aspx");
        }
            //code for the users
        else if (TextBoxUsername.Text != "" && TextBoxPassword.Text != "")
        {
            Session["name"] = TextBoxUsername.Text;
            Response.Redirect("UserPage.aspx");

        }
        else
        {
            Label3.Text = "Please enter the UserName and Password";
            this.Label3.ForeColor = Color.Red;
        }
    }
}

LogOut.aspx

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

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
LogOut.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class LogOut : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Remove("name");
        Response.Redirect("Login.aspx");
    }
}

UserPage.aspx

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

<!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>Untitled Page</title>
</head>
<body bgcolor="#ccccff">
    <form id="form1" runat="server">
    <div>
        <table style="height: 76px" width="950">
            <tr>
                <td align="center">
                    <asp:Label ID="Label1" runat="server" Text="WELCOME " Width="169px"></asp:Label>
                    <asp:Label ID="Label2" runat="server" Font-Size="X-Large" ForeColor="Maroon" Width="186px"></asp:Label></td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="LogOut" Width="92px" /></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

UserPage.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label2.Text = Session["name"].ToString();
        if (Label2.Text == null)
        {
            Response.Redirect("Login.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("LogOut.aspx");
    }
}




Source Code

Author :
Revathy M S
Joined Date :12-Jun-2014
Articles
Posted
3
Messages
Posted
0

   
Messages
Posted:
Post Your Comments
Name (Max 50 Chars)
Comments

TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud