Asses-2 3
Asses-2 3
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>ViewState Demo</h2>
<asp:Label ID="lblName" runat="server" Text="Enter Name: "></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnSave" runat="server" Text="Save to ViewState"
OnClick="btnSave_Click" />
<asp:Button ID="btnShow" runat="server" Text="Show from ViewState"
OnClick="btnShow_Click" />
<br /><br />
<asp:Label ID="lblResult" runat="server" Font-Bold="True"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Assess_2._3
{
public partial class ViewStateDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
ViewState["UserName"] = txtName.Text;
lblResult.Text = "Name saved in ViewState.";
}
}
}
Output:
b) Pass values between two pages using QueryString and display them on the
second page.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>QueryString Demo</h2>
<asp:Label ID="Label1" runat="server" Text="Enter Message: "></asp:Label>
<asp:TextBox ID="txtMessage" runat="server"></asp:TextBox>
<asp:Button ID="btnSend" runat="server" Text="Go to Page 2"
OnClick="btnSend_Click" />
</div>
</form>
</body>
</html>
� Page1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Assess_2._3
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
string message = txtMessage.Text;
Response.Redirect("Page2.aspx?msg=" + Server.UrlEncode(message));
}
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Received Message</h2>
<asp:Label ID="lblMessage" runat="server" Font-Bold="True"
ForeColor="Blue"></asp:Label>
</div>
</form>
</body>
</html>
� Page2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Assess_2._3
{
public partial class Page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["msg"] != null)
{
lblMessage.Text = "Message received: " +
Server.UrlDecode(Request.QueryString["msg"]);
}
}
}
}
Output:
c) Store user login info in a Session Variable and use it to control access
to pages.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Login</h2>
<asp:Label ID="lblUser" runat="server" Text="Username:
"></asp:Label>
<asp:TextBox ID="txtUser" runat="server"></asp:TextBox>
<asp:Button ID="btnLogin" runat="server" Text="Login"
OnClick="btnLogin_Click" />
<asp:Label ID="lblStatus" runat="server"
ForeColor="Red"></asp:Label>
</div>
</form>
</body>
</html>
� Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Assess_2._3
{
public partial class Login1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
string user = txtUser.Text;
// Dummy authentication
if (user == "admin")
{
Session["Username"] = user;
Response.Redirect("Dashboard.aspx");
}
else
{
lblStatus.Text = "Invalid login.";
}
}
}
� Step 2: Create a Dashboard Page – 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>
<h2>Welcome to Dashboard</h2>
<asp:Label ID="lblUser" runat="server" Font-Bold="True"
ForeColor="Green"></asp:Label>
<asp:Button ID="btnLogout" runat="server" Text="Logout"
OnClick="btnLogout_Click" />
</div>
</form>
</body>
</html>
� Dashboard.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Assess_2._3
{
public partial class Dashboard : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] != null)
{
lblUser.Text = "Logged in as: " + Session["Username"].ToString();
}
else
{
Response.Redirect("Login.aspx");
}
}
}
}
Output: