In: Computer Science
Web Application Development Course - C#
Update the ASP.NET web application code by adding below functionality:
• Create a new web form called Welcome.aspx and add labels to show name, company, email, and membership.
• Create a new business layer class called Customer to hold the entered user details
• When the user clicks on Sign Up button and if all the validations on the page pass
o Store the details entered on page in the Customer object.
o Save the Customer object in Session State.
o Navigate to Welcome.aspx, read the Customer object from Session State and show the name, company, email and membership on this page using respective labels.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PopQuiz4
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
if (!IsPostBack)
{
radBtnFree.Checked = true;
ddlTimeZone.SelectedIndex = 0;
}
}
protected void radBtnFree_CheckedChanged(object sender,
EventArgs e)
{
if (radBtnFree.Checked)
{
lblAmount.Text = "$0";
}
}
protected void radBtnBasic_CheckedChanged(object sender,
EventArgs e)
{
if (radBtnBasic.Checked)
{
lblAmount.Text = "$30";
}
}
protected void radBtnPremium_CheckedChanged(object sender,
EventArgs e)
{
if (radBtnPremium.Checked)
{
lblAmount.Text = "$60";
}
}
protected void CustValTxtEmail_ServerValidate(object source,
ServerValidateEventArgs args)
{
if (txtEmail.Text.Contains(txtCompany.Text))
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
protected void btnSignup_Click(object sender, EventArgs e)
{
if (IsValid)
{
lblStatus.Text = "Sign up successful!";
lblStatus.ForeColor = System.Drawing.Color.Green;
}
}
}
}
Hi,
Please find the Class and pages below:
SignUp.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SignUp.aspx.cs" Inherits="DisplayInfoFromDB.SignUp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Name:
<asp:TextBox ID="txtName" runat="server" Height="20px" Width="200px"></asp:TextBox>
<br />
</div>
<div>
Company:
<asp:TextBox ID="txtCompany" runat="server" Height="20px" Width="200px"></asp:TextBox>
<br />
</div>
<div>
Email:
<asp:TextBox ID="txtEmail" runat="server" Height="20px" Width="200px"></asp:TextBox>
<br />
</div>
<div>
Membership:
<asp:TextBox ID="txtMembership" runat="server" Height="20px" Width="200px"></asp:TextBox>
<br />
</div>
<br /><br />
<asp:Button Text="Sign Up" ID="btnSignUp" runat="server" Height="20px" Width="100px" OnClick="btnSignUp_Click" />
</form>
</body>
</html>
SignUp.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DisplayInfoFromDB
{
public partial class SignUp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSignUp_Click(object sender, EventArgs e)
{ //get the user input from text boxes to string variables
string sUserName = txtName.Text.Trim();
string sCompanyName = txtCompany.Text.Trim();
string sEmail = txtEmail.Text.Trim();
string sMembership = txtMembership.Text.Trim();
if (IsValid)
{
//Instantiate Class of Customer
Customer oCustomer = new Customer();
//Set Propertis of Cusomer class using text entered by user
oCustomer.UserName = sUserName;
oCustomer.CompanyName = sCompanyName;
oCustomer.Email = sEmail;
oCustomer.Membership = sMembership;
//Crate Session objecct
Session["Customer"] = oCustomer;
//Redirect to welcome page
Response.Redirect("~/Welcome.aspx");
}
}
}
}
Welcome.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="DisplayInfoFromDB.Welcome" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Name:
<asp:Label ID="lblName" runat="server" Height="20px" Width="200px"></asp:Label>
<br />
</div>
<div>
Company:
<asp:Label ID="lblCompany" runat="server" Height="20px" Width="200px"></asp:Label>
<br />
</div>
<div>
Email:
<asp:Label ID="lblEmail" runat="server" Height="20px" Width="200px"></asp:Label>
<br />
</div>
<div>
Membership:
<asp:Label ID="lblMembership" runat="server" Height="20px" Width="200px"></asp:Label>
<br />
</div>
</form>
</body>
</html>
Welcome.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
namespace DisplayInfoFromDB
{
public partial class Welcome : Customer //IMplement the Custome Class
{
/// <summary>
/// Page Load Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void Page_Load(object sender, EventArgs e)
{
//First call out base class's page load method and will check the session is live or not
base.Page_Load(sender, e);
//Call function to fetch data
//here you are confirm that your session is live and session object is having data
FetchInforAndDisplay();
}
/// <summary>
/// Function to fetch data from database and display to form
/// </summary>
private void FetchInforAndDisplay()
{
try
{ //check session is not null
if (!string.IsNullOrEmpty(Session["Customer"].ToString()))
{ //fetch the Object of Customer Class from Sesson variable and cast it to Customer type
Customer oCustomer = (Customer)Session["Customer"];
// fetch the data from the Custome Class session object and set it to form
lblName.Text = oCustomer.UserName;
lblCompany.Text = oCustomer.CompanyName;
lblEmail.Text = oCustomer.Email;
lblMembership.Text = oCustomer.Membership;
}
else { //IF sessin is is null then return user to signup page
Response.Redirect("~/SignUp.aspx");
}
}
catch (Exception ex)
{ // if any exception then reutn uer to sign up page
Response.Redirect("~/SignUp.aspx");
}
}
}
}
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DisplayInfoFromDB
{
public class Customer : System.Web.UI.Page
{
public string UserName = string.Empty;
public string CompanyName = string.Empty;
public string Email = string.Empty;
public string Membership = string.Empty;
protected virtual void Page_Load(object sender, EventArgs e)
{
try
{
if (Session["Customer"] != null && !string.IsNullOrEmpty(Session["Customer"].ToString()))
{
//Her you can do the things to set application level
}
else
{
//you need to create new login page here so once session is lost then it will rediret to the login page
//rigt now i have redirect it to sign up
//clears the session and redirect user to signup page
Session.Abandon();
Response.Redirect("~/SignUp.aspx");
}
}
catch (Exception ex)
{ //if any exeception comes the clear all the session and redirect user to signup page
Session.Abandon();
Response.Redirect("~/SignUp.aspx");
}
}
}
}
Customer class is the Base Class for all your pages. If you are going to create any new pages in the application then that respected page's base class should be Customer Class. You can see the Welcome.aspx.cs Class which base class is Customer Class.
In this Customer Class, we have handled the Session i.e. we have checked that session is available then only return to that requested page otherwise you will be redirected to the login/sign-up page.
in the above application, if you directly browse the Welcome.aspx page, then it will redirect you to the signup page because the session is not created for the user.
Thanks