0% found this document useful (0 votes)
32 views15 pages

Asses-2 3

The document outlines a series of ASP.NET projects demonstrating the use of ViewState, QueryString, and Session variables for data management in web applications. It includes code examples for creating forms, passing values between pages, and managing user login sessions. The projects aim to illustrate how to store and retrieve data effectively in a web environment.

Uploaded by

vishalsandhan178
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views15 pages

Asses-2 3

The document outlines a series of ASP.NET projects demonstrating the use of ViewState, QueryString, and Session variables for data management in web applications. It includes code examples for creating forms, passing values between pages, and managing user login sessions. The projects aim to illustrate how to store and retrieve data effectively in a web environment.

Uploaded by

vishalsandhan178
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Assessment-2.

Name: Siddhant Kiran Shahane


Reg. No.: wlgetl3dot001
University Name: D Y Patil College of Engineering
Email: [email protected]
Contact: 8446539240
Level: 3
Subject Name: Dot-net
(3)
a) Use ViewState to store and retrieve form values between postbacks.

1. Creating new asp.net project

a) Using ViewState to Store and Retrieve Form Values Between Postbacks

� Step 1: Create a Form in ViewStateDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewStateDemo.aspx.cs"


Inherits="Assess_2._3.ViewStateDemo" %>

<!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>

� Step 2: Code-behind for ViewState (ViewStateDemo.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 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.";
}

protected void btnShow_Click(object sender, EventArgs e)


{
if (ViewState["UserName"] != null)
{
lblResult.Text = "Stored Name: " +
ViewState["UserName"].ToString();
}
else
{
lblResult.Text = "No name stored in ViewState.";
}
}

}
}
Output:
b) Pass values between two pages using QueryString and display them on the
second page.

� Step 1: Create Page1.aspx (Sender Page)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs"


Inherits="Assess_2._3.Login" %>

<!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));
}

}
}

� Step 2: Create Page2.aspx (Receiver Page)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page2.aspx.cs"


Inherits="Assess_2._3.Page2" %>

<!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.

� Step 1: Create a Login Page – Login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs"


Inherits="Assess_2._3.Login1" %>

<!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

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dashboard.aspx.cs"


Inherits="Assess_2._3.Dashboard" %>

<!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");
}
}

protected void btnLogout_Click(object sender, EventArgs e)


{
Session.Abandon();
Response.Redirect("Login.aspx");
}

}
}
Output:

You might also like