Tuesday, January 22, 2013

Example for assignment 2

The master page and css are the same as in the previous post

Default.aspx


<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <!--This is the body content-->
    <h2>Make A Donation</h2>
    <table>
        <tr>
            <td>Enter First Name</td>
            <td>
                <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter Last Name</td>
            <td>
                <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtLastName" ErrorMessage="Last Name is required" CssClass="errMessage" Display="None" ViewStateMode="Disabled"></asp:RequiredFieldValidator>
             </td>
        </tr>
         <tr>
            <td>Enter Street Address</td>
            <td>
                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter City</td>
            <td>
                <asp:TextBox ID="txtCity" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter State</td>
            <td>
                <asp:TextBox ID="txtState" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter Zip Code</td>
            <td>
                <asp:TextBox ID="txtZip" runat="server"></asp:TextBox></td>
            <td>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtZip" ErrorMessage="Invalid zip code" ValidationExpression="\d{5}(-\d{4})?"  CssClass="errMessage"></asp:RegularExpressionValidator>
             </td>
        </tr>
         <tr>
            <td>Enter Email</td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
            <td>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtEmail" ErrorMessage="Invalid Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" CssClass="errMessage"></asp:RegularExpressionValidator>
             </td>
        </tr>
         <tr>
            <td>Enter Phone</td>
            <td>
                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter DonationAmount</td>
            <td>
                <asp:TextBox ID="txtDonation" runat="server"></asp:TextBox></td>
            <td>
                <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtDonation" ErrorMessage="Must be a numeric value" Type="Double"></asp:CompareValidator>
             </td>
        </tr>
         <tr>
            <td>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
            <td>
                <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
                </td>
            <td>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
             </td>
        </tr>
    </table>
</asp:Content>



Default.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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //instantiate the Donor object
        Donor d = new Donor();
        //assign the values from the text fields to the poperties
        d.FirstName = txtFirstName.Text;
        d.LastName = txtLastName.Text;
        d.Address = txtAddress.Text;
        d.City = txtCity.Text;
        d.State = txtState.Text;
        d.Zip = txtZip.Text;
        d.Phone = txtPhone.Text;
        d.Email = txtEmail.Text;
        d.DonationAmt = double.Parse(txtDonation.Text);

    //save the object to a session variable
        Session["NewDonor"] = d;

        //Redirect to the next page
        Response.Redirect("Default2.aspx");

    }
}

Donor.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// 
/// Summary description for Donor
/// 
public class Donor
{
    //private variables
    private string lastName;
    private string firstName;
    private string address;
    private string city;
    private string state;
    private string zip;
    private string email;
    private string phone;
    private double donationAmt;

    #region public properties
    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public string Address
    {
        get { return address; }
        set { address = value; }
    }


    public string City
    {
        get { return city; }
        set { city = value; }
    }


    public string State
    {
        get { return state; }
        set { state = value; }
    }


    public string Zip
    {
        get { return zip; }
        set { zip = value; }
    }


    public string Email
    {
        get { return email; }
        set { email = value; }
    }


    public string Phone
    {
        get { return phone; }
        set { phone = value; }
    }


    public double DonationAmt
    {
        get { return donationAmt; }
        set { donationAmt = value; }
    }

    public string FirstName
    {
        get
        {
            return firstName;
        }
        set
        {
            firstName = value;
        }
    }
#endregion

    public Donor()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    //public property
}

Default2.aspx


<%@ Page Title="Confirm Information" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <h2>Check your info</h2>
    <table>
        <tr>
            <td>Enter First Name</td>
            <td>
                <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter Last Name</td>
            <td>
                <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtLastName" ErrorMessage="Last Name is required" CssClass="errMessage" Display="None" ViewStateMode="Disabled"></asp:RequiredFieldValidator>
             </td>
        </tr>
         <tr>
            <td>Enter Street Address</td>
            <td>
                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter City</td>
            <td>
                <asp:TextBox ID="txtCity" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter State</td>
            <td>
                <asp:TextBox ID="txtState" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter Zip Code</td>
            <td>
                <asp:TextBox ID="txtZip" runat="server"></asp:TextBox></td>
            <td>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtZip" ErrorMessage="Invalid zip code" ValidationExpression="\d{5}(-\d{4})?"  CssClass="errMessage"></asp:RegularExpressionValidator>
             </td>
        </tr>
         <tr>
            <td>Enter Email</td>
            <td>
                <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
            <td>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtEmail" ErrorMessage="Invalid Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" CssClass="errMessage"></asp:RegularExpressionValidator>
             </td>
        </tr>
         <tr>
            <td>Enter Phone</td>
            <td>
                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox></td>
            <td></td>
        </tr>
         <tr>
            <td>Enter DonationAmount</td>
            <td>
                <asp:TextBox ID="txtDonation" runat="server"></asp:TextBox></td>
            <td>
                 </td>
        </tr>
         <tr>
            <td>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
            <td>
                <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
                </td>
            <td>
                <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
             </td>
        </tr>
    </table>
</asp:Content>



Default2.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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["NewDonor"] != null)
        {
            Donor d = (Donor)Session["NewDonor"];
            txtFirstName.Text = d.FirstName;
            txtLastName.Text = d.LastName;
            txtAddress.Text = d.Address;
            txtCity.Text = d.City;
            txtState.Text = d.State;
            txtZip.Text = d.Zip;
            txtPhone.Text = d.Phone;
            txtEmail.Text = d.Email;
            txtDonation.Text = d.DonationAmt.ToString();
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string lName=txtLastName.Text;
        string amount=txtDonation.Text;
        Response.Redirect("Default3.aspx?name=" +
            lName + "&amount=" + amount);
    }
}

Default3.aspx


<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Content>



Default3.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 Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string lastName = Request.QueryString["name"];
        string donation = Request.QueryString["amount"];
        Label1.Text = "thank you, " + lastName + " for your donation of "
            + donation;
    }
}

WebConfig

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
  </appSettings>
</configuration>

No comments:

Post a Comment