As I said before,,, this works fine in Visual Studio 2010.
I am using Visual Studio Web Application.
Master Page:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Chapter 9: Master Page</title>
<style type="text/css">
.Row1Column1
{
width: 160px;
height: 400px;
border-color: Red;
background-color: Red;
}
.Row1Column2
{
width: 10px;
height: 400px;
}
.Row1Column3
{
width: 520px;
height: 400px;
}
.Row2Column1
{
width: 160px;
height: 25px;
border-color: Red;
background-color: Red;
}
.Row2Column2
{
width: 10;
height: 25;
}
.Row2Column3
{
width: 520px;
height: 25;
}
</style>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/banner.jpg" />
<br />
<table cellpadding="2" cellspacing="0">
<tr>
<td class="Row1Column1" valign="top">
<br />
<asp:HyperLink ID="HyperLink1" runat="server"
ForeColor="White"
NavigateUrl="~/Order.aspx">Home</asp:HyperLink>
<br />
<br />
<asp:HyperLink ID="HyperLink2" runat="server"
ForeColor="White" NavigateUrl="~/Cart.aspx">
Your Shopping Cart</asp:HyperLink>
<br />
<br />
<asp:HyperLink ID="HyperLink3" runat="server"
ForeColor="White" NavigateUrl="~/Service.aspx">
Customer Service</asp:HyperLink>
<br />
<br />
<asp:HyperLink ID="HyperLink4" runat="server"
ForeColor="White"
NavigateUrl="~/About.aspx">About Us</asp:HyperLink>
</td>
<td class="Row1Column2"></td>
<td class="Row1Column3" valign="top">
<asp:ContentPlaceHolder id="Main" runat="server"></asp:ContentPlaceHolder>
</td>
</tr>
<tr>
<td class="Row2Column1"></td>
<td class="Row2Column2"></td>
<td class="Row2Column3">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
int daysUntil = DaysUntilHalloween();
if (daysUntil == 0)
lblMessage.Text = "Happy Halloween!";
else if (daysUntil == 1)
lblMessage.Text = "Tomorrow is Halloween!";
else
lblMessage.Text = "There are only " + daysUntil
+ " days left until Halloween!";
}
private int DaysUntilHalloween()
{
DateTime halloween = new DateTime(DateTime.Today.Year, 10, 31);
if (DateTime.Today > halloween)
halloween = halloween.AddYears(1);
TimeSpan ts = halloween - DateTime.Today;
return ts.Days;
}
}
ORDER.ASPX
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Order.aspx.cs" Inherits="Order" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
.style1
{
width: 250px;
}
.style2
{
width: 20px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Main" Runat="Server">
<br />
<asp:Label ID="Label1" runat="server"
Text="Please select a product:"></asp:Label>
<asp:DropDownList ID="ddlProducts" runat="server"
DataSourceID="AccessDataSource1" DataTextField="Name"
DataValueField="ProductID" Width="150px" AutoPostBack="True">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Halloween.mdb"
SelectCommand="SELECT [ProductID], [Name], [ShortDescription],
[LongDescription], [ImageFile], [UnitPrice]
FROM [Products] ORDER BY [Name]">
</asp:AccessDataSource>
<br />
<table>
<tr>
<td class="style1">
<asp:Label ID="lblName" runat="server"
style="font-weight: 700; font-size: larger">
</asp:Label>
</td>
<td class="style2" rowspan="4">
</td>
<td rowspan="4" valign="top">
<asp:Image ID="imgProduct" runat="server" Height="200px" />
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="lblShortDescription" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="lblLongDescription" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="lblUnitPrice" runat="server"
style="font-weight: 700; font-size: larger">
</asp:Label>
<asp:Label ID="Label2" runat="server" Text="each"
style="font-weight: 700; font-size: larger">
</asp:Label>
</td>
</tr>
</table>
<br />
<asp:Label ID="Label3" runat="server" Text="Quantity:"
Width="80px"></asp:Label>
<asp:TextBox ID="txtQuantity" runat="server" Width="80px">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="txtQuantity" Display="Dynamic"
ErrorMessage="Quantity is a required field.">
</asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="txtQuantity" Display="Dynamic"
ErrorMessage="Quantity must range from 1 to 500."
MaximumValue="500" MinimumValue="1" Type="Integer">
</asp:RangeValidator><br /><br />
<asp:Button ID="btnAdd" runat="server" Text="Add to Cart"
OnClick="btnAdd_Click" />
<asp:Button ID="btnCart" runat="server" CausesValidation="False"
PostBackUrl="~/Cart.aspx" Text="Go to Cart" />
</asp:Content>
CODE BEHIND
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Order : System.Web.UI.Page
{
private Product selectedProduct;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ddlProducts.DataBind();
selectedProduct = this.GetSelectedProduct();
lblName.Text = selectedProduct.Name;
lblShortDescription.Text = selectedProduct.ShortDescription;
lblLongDescription.Text = selectedProduct.LongDescription;
lblUnitPrice.Text = selectedProduct.UnitPrice.ToString("c");
imgProduct.ImageUrl = "Images/Products/" + selectedProduct.ImageFile;
}
private Product GetSelectedProduct()
{
DataView productsTable = (DataView)
AccessDataSource1.Select(DataSourceSelectArguments.Empty);
productsTable.RowFilter =
"ProductID = '" + ddlProducts.SelectedValue + "'";
DataRowView row = (DataRowView) productsTable[0];
Product p = new Product();
p.ProductID = row["ProductID"].ToString();
p.Name = row["Name"].ToString();
p.ShortDescription = row["ShortDescription"].ToString();
p.LongDescription = row["LongDescription"].ToString();
p.UnitPrice = (decimal) row["UnitPrice"];
p.ImageFile = row["ImageFile"].ToString();
return p;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
CartItem item = new CartItem();
item.Product = selectedProduct;
item.Quantity = Convert.ToInt32(txtQuantity.Text);
this.AddToCart(item);
Response.Redirect("Cart.aspx");
}
}
private void AddToCart(CartItem item)
{
SortedList cart = this.GetCart();
string productID = selectedProduct.ProductID;
if (cart.ContainsKey(productID))
{
CartItem existingItem = (CartItem) cart[productID];
existingItem.Quantity += item.Quantity;
}
else
cart.Add(productID, item);
}
private SortedList GetCart()
{
if (Session["Cart"] == null)
Session.Add("Cart", new SortedList());
return (SortedList) Session["Cart"];
}
}