<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Session_State_Practice.WebForm1" %>
<!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: 123px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellpadding="3" cellspacing="4" class="auto-style1">
<tr>
<td class="auto-style2">USERNAME</td>
<td>
<asp:TextBox ID="UserTextBox" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="SubmitButton" runat="server" Text="SUBMIT" OnClick="SubmitButton_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Above File is Session_State_Practice\WebForm1.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Session_State_Practice
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
//Below is First way for creating session variable
//Session.Add("user",UserTextBox.Text);
//Below is Second way for creating session variable
//Below Second way is most common way for creating session variable
Session["user"] = UserTextBox.Text;
Response.Redirect("WebForm2.aspx");
}
}
}
Above File is Session_State_Practice\WebForm1.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Session_State_Practice.WebForm2" %>
<!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="~/WebForm3.aspx">GO TO PAGE 3</asp:LinkButton>
</div>
</form>
</body>
</html>
Above File is Session_State_Practice\WebForm2.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Session_State_Practice
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] != null)
{
//Generally we use "Response.Write()" when we want to display message at top left side in page
Response.Write("Welcome " + Session["user"].ToString());
}
else
{
Response.Redirect("WebForm1.aspx");
}
}
}
}
Above File is Session_State_Practice\WebForm2.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Session_State_Practice.WebForm3" %>
<!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="LogoutButton" runat="server" Text="LOGOUT" OnClick="LogoutButton_Click"/>
</div>
</form>
</body>
</html>
Above File is Session_State_Practice\WebForm3.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Session_State_Practice
{
public partial class WebForm3 : 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("WebForm1.aspx");
}
}
protected void LogoutButton_Click(object sender, EventArgs e)
{
if (Session["user"] != null)
{
Session["user"] = null;
Response.Redirect("WebForm1.aspx");
}
}
}
}
Above File is Session_State_Practice\WebForm3.aspx.cs
Comments
Post a Comment