Tuesday, March 19, 2013

Web Page Examples

First we did the wizard to get all the products. Here is the source code for Default.aspx<>p>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Our Products</h1>
        <asp:DataList ID="DataList1" runat="server" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333">
            <AlternatingItemStyle BackColor="White" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <ItemStyle BackColor="#E3EAEB" />
            <ItemTemplate>
                <strong>
                <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' /></strong>
                <br />
                $
                <asp:Label ID="ProductUnitPriceLabel" runat="server" Text='<%# Eval("ProductUnitPrice") %>' />
                <br />

            </ItemTemplate>
            <SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PerfectPizzaConnectionString %>" SelectCommand="SELECT [ProductName], cast([ProductUnitPrice] as decimal(5,2)) as [ProductUnitPrice] FROM [Product] ORDER BY [ProductName]"></asp:SqlDataSource>
    </div>
        <p>Enter your phone number to order or register if you are a new customer  <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
        </p>
        <asp:LinkButton ID="LinkButton1" runat="server">Register</asp:LinkButton>
    </form>
</body>
</html>

Here is the code behind which checks to see if the phone number matches

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 Button1_Click(object sender, EventArgs e)
    {
        PerfectPizzaEntities pe = new PerfectPizzaEntities();
        var ph = from p in pe.Customers
                 where p.CustomerPhoneKey.Equals(txtPhone.Text)
                 select p.CustomerPhoneKey;
        if (ph.ToList().Count != 0)
            Response.Redirect("Default3.aspx");
        else
            Response.Redirect("Default4.aspx");
                
    }
}

Here is the form for entering a new customer

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>Register</h1>
        <table>
            <tr>
                <td>Enter phone</td>
                <td>
                    <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>Last Name</td>
                <td>
                    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>Address1</td>
                <td>
                    <asp:TextBox ID="txtAddress1" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>Address2</td>
                <td>
                    <asp:TextBox ID="txtAddress2" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>City</td>
                <td>
                    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>State</td>
                <td>
                    <asp:TextBox ID="txtState" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>Zip Code</td>
                <td>
                    <asp:TextBox ID="txtZip" runat="server"></asp:TextBox> </td>
            </tr>
                       <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /></td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>  </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

And here is the code behind

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

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        PerfectPizzaEntities pe = new PerfectPizzaEntities();
        Customer c = new Customer();
        c.CustomerPhoneKey = txtPhone.Text;
        c.CustomerLastName = txtLastName.Text;
        c.CustomerAddress1 = txtAddress1.Text;
        c.CustomerAddress2 = txtAddress2.Text;
        c.CustomerCity = txtCity.Text;
        c.CustomerState = txtState.Text;
        c.CustomerZip = txtZip.Text;
        pe.Customers.Add(c);
        pe.SaveChanges();

        Response.Redirect("Default2.aspx");
    }
}

No comments:

Post a Comment