Asp.Net Session Practical
CODE: login.aspx
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
User Name
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
CODE: login.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 Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtUsername.Text) && txtPassword.Text.ToLower() == "abc12")
{
Session["LogUserName"] = txtUsername.Text;
Response.Redirect("dashboard.aspx");
}
}
}
CODE: dashboard.aspx
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Welcome <asp:Label ID="lblUserName" runat="server"></asp:Label> </h1>
</div>
</form>
</body>
</html>
CODE: dashboard.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 dashboard : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string UserName = Convert.ToString(Session["LogUserName"]);
if (!string.IsNullOrEmpty(UserName))
{
lblUserName.Text = UserName;
}
else
{
Response.Redirect("Login.aspx");
}
}
}