congo.aspx和congo.cs和ViewCart.aspx

ShoppingCart 是Congo.cs中定义的一种自定义数据类型。它还伴随着BookOrder,也是在Congo.cs中定义。
ShoppingCart主要是对HashTable的包装。
它实现了一个名为_Orders的私有字段,该字段持有一个HashTable引用,ShoppingCart 还实现了一些公有方法这些方法使BookOrder对象能够被添加到HashTable中并被删除,
ShoppingCart还实现了一个名为Orders的公共属性,该属性在HashTable的ICollection接口中可用:
aspx:

congo.aspx和congo.cs和ViewCart.aspx

congo.aspx.cs:
congo.aspx和congo.cs和ViewCart.aspx using  System;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Data;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Data.SqlClient;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Configuration;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Collections;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Web;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Web.Security;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Web.UI;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Web.UI.WebControls;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Web.UI.WebControls.WebParts;
congo.aspx和congo.cs和ViewCart.aspx
using  System.Web.UI.HtmlControls;
congo.aspx和congo.cs和ViewCart.aspx
congo.aspx和congo.cs和ViewCart.aspx
public  partial  class  congo : System.Web.UI.Page
congo.aspx和congo.cs和ViewCart.aspx
{
congo.aspx和congo.cs和ViewCart.aspx    
protected void Page_Load(object sender, EventArgs e)
congo.aspx和congo.cs和ViewCart.aspx    
{
congo.aspx和congo.cs和ViewCart.aspx
congo.aspx和congo.cs和ViewCart.aspx        
if (!IsPostBack)
congo.aspx和congo.cs和ViewCart.aspx        
{
congo.aspx和congo.cs和ViewCart.aspx           
congo.aspx和congo.cs和ViewCart.aspx            
string ConnectString = ConfigurationSettings.AppSettings["connectString"];
congo.aspx和congo.cs和ViewCart.aspx            SqlDataAdapter adapter 
= new SqlDataAdapter("select * from titles where price!=0", ConnectString);
congo.aspx和congo.cs和ViewCart.aspx            DataSet ds 
= new DataSet();
congo.aspx和congo.cs和ViewCart.aspx            adapter.Fill(ds);
congo.aspx和congo.cs和ViewCart.aspx            MyDataGrid.DataSource 
= ds;
congo.aspx和congo.cs和ViewCart.aspx            MyDataGrid.DataBind();
congo.aspx和congo.cs和ViewCart.aspx        }

congo.aspx和congo.cs和ViewCart.aspx    }

congo.aspx和congo.cs和ViewCart.aspx
congo.aspx和congo.cs和ViewCart.aspx    
//用户调用该页面并单击“AddToCart”按钮时调用该方法
congo.aspx和congo.cs和ViewCart.aspx
    /// <summary>
congo.aspx和congo.cs和ViewCart.aspx    
/// 该方法从DataGrid中检索产品ID及相应书籍的价格,并将
congo.aspx和congo.cs和ViewCart.aspx    
/// 它们封装到一个BookOrder对象中
congo.aspx和congo.cs和ViewCart.aspx    
/// </summary>
congo.aspx和congo.cs和ViewCart.aspx    
/// <param name="sender"></param>
congo.aspx和congo.cs和ViewCart.aspx    
/// <param name="e"></param>

congo.aspx和congo.cs和ViewCart.aspx    protected void OnItemCommand(Object sender,  DataGridCommandEventArgs e)
congo.aspx和congo.cs和ViewCart.aspx    
{
congo.aspx和congo.cs和ViewCart.aspx        
if (e.CommandName == "AddToCart")
congo.aspx和congo.cs和ViewCart.aspx        
{
congo.aspx和congo.cs和ViewCart.aspx            
//有两种方法可以得到这里的BookOrder、ShoppingCart
congo.aspx和congo.cs和ViewCart.aspx            
//1.把congo.cs放在App_Code文件夹内。
congo.aspx和congo.cs和ViewCart.aspx            
//2.把congo.cs编译成dll文件放在Bin文件夹内
congo.aspx和congo.cs和ViewCart.aspx            
//倾向于前者,便于查看Congo.cs的内容
congo.aspx和congo.cs和ViewCart.aspx
            BookOrder order =
congo.aspx和congo.cs和ViewCart.aspx                
new BookOrder(e.Item.Cells[0].Text,e.Item.Cells[1].Text,Convert.ToDecimal(e.Item.Cells[2].Text.Substring(1)),1);
congo.aspx和congo.cs和ViewCart.aspx
congo.aspx和congo.cs和ViewCart.aspx            
//global.asax中有Session_Start,这里的SessionID????
congo.aspx和congo.cs和ViewCart.aspx
            ShoppingCart cart = (ShoppingCart)Session["MyShoppingCart"];
congo.aspx和congo.cs和ViewCart.aspx            
if (cart != null)
congo.aspx和congo.cs和ViewCart.aspx            
{
congo.aspx和congo.cs和ViewCart.aspx                cart.AddOrder(order);
congo.aspx和congo.cs和ViewCart.aspx            }

congo.aspx和congo.cs和ViewCart.aspx            Response.Write(
"<script>alert('success to add "+e.Item.Cells[1].Text+"')</script>");
congo.aspx和congo.cs和ViewCart.aspx        }

congo.aspx和congo.cs和ViewCart.aspx    }

congo.aspx和congo.cs和ViewCart.aspx
congo.aspx和congo.cs和ViewCart.aspx    
protected void OnViewCart(Object sender, EventArgs e)
congo.aspx和congo.cs和ViewCart.aspx    
{
congo.aspx和congo.cs和ViewCart.aspx        Response.Redirect(
"ViewCart.aspx");
congo.aspx和congo.cs和ViewCart.aspx    }

congo.aspx和congo.cs和ViewCart.aspx}

congo.aspx和congo.cs和ViewCart.aspx

global.asax:
congo.aspx和congo.cs和ViewCart.aspx <% @ Application Language = " C# "   %>
congo.aspx和congo.cs和ViewCart.aspx
congo.aspx和congo.cs和ViewCart.aspx
< script runat = " server " >
congo.aspx和congo.cs和ViewCart.aspx
congo.aspx和congo.cs和ViewCart.aspx    
void  Application_Start( object  sender, EventArgs e) 
congo.aspx和congo.cs和ViewCart.aspx    
{
congo.aspx和congo.cs和ViewCart.aspx        
// Code that runs on application startup
congo.aspx和congo.cs和ViewCart.aspx

congo.aspx和congo.cs和ViewCart.aspx    }

congo.aspx和congo.cs和ViewCart.aspx    
congo.aspx和congo.cs和ViewCart.aspx    
void  Application_End( object  sender, EventArgs e) 
congo.aspx和congo.cs和ViewCart.aspx    
{
congo.aspx和congo.cs和ViewCart.aspx        
//  Code that runs on application shutdown
congo.aspx和congo.cs和ViewCart.aspx

congo.aspx和congo.cs和ViewCart.aspx    }

congo.aspx和congo.cs和ViewCart.aspx        
congo.aspx和congo.cs和ViewCart.aspx    
void  Application_Error( object  sender, EventArgs e) 
congo.aspx和congo.cs和ViewCart.aspx    

congo.aspx和congo.cs和ViewCart.aspx        
// Code that runs when an unhandled error occurs
congo.aspx和congo.cs和ViewCart.aspx

congo.aspx和congo.cs和ViewCart.aspx    }

congo.aspx和congo.cs和ViewCart.aspx
congo.aspx和congo.cs和ViewCart.aspx    
void  Session_Start( object  sender, EventArgs e) 
congo.aspx和congo.cs和ViewCart.aspx    
{
congo.aspx和congo.cs和ViewCart.aspx       
//为请求页面的用户创建一个会话
congo.aspx和congo.cs和ViewCart.aspx
        Session["MyShoppingCart"= new ShoppingCart();
congo.aspx和congo.cs和ViewCart.aspx        
// Code that runs when a new session is started
congo.aspx和congo.cs和ViewCart.aspx

congo.aspx和congo.cs和ViewCart.aspx    }

congo.aspx和congo.cs和ViewCart.aspx
congo.aspx和congo.cs和ViewCart.aspx    
void  Session_End( object  sender, EventArgs e) 
congo.aspx和congo.cs和ViewCart.aspx    
{
congo.aspx和congo.cs和ViewCart.aspx        
// Code that runs when a session ends. 
congo.aspx和congo.cs和ViewCart.aspx        
// Note: The Session_End event is raised only when the sessionstate mode
congo.aspx和congo.cs和ViewCart.aspx        
// is set to InProc in the Web.config file. If session mode is set to StateServer 
congo.aspx和congo.cs和ViewCart.aspx        
// or SQLServer, the event is not raised.
congo.aspx和congo.cs和ViewCart.aspx

congo.aspx和congo.cs和ViewCart.aspx    }

congo.aspx和congo.cs和ViewCart.aspx       
congo.aspx和congo.cs和ViewCart.aspx
</ script >
congo.aspx和congo.cs和ViewCart.aspx
congo.cs(独立代码,在App_Code)中
congo.aspx和congo.cs和ViewCart.aspx

ViewCart.aspx和aspx.cs:
 1 congo.aspx和congo.cs和ViewCart.aspx <% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " ViewCart.aspx.cs "  Inherits = " ViewCart "   %>
 2 congo.aspx和congo.cs和ViewCart.aspx
 3 congo.aspx和congo.cs和ViewCart.aspx
 4 congo.aspx和congo.cs和ViewCart.aspx < html xmlns = " http://www.w3.org/1999/xhtml "   >
 5 congo.aspx和congo.cs和ViewCart.aspx < head runat = " server " >
 6 congo.aspx和congo.cs和ViewCart.aspx     < title > Untitled Page </ title >
 7 congo.aspx和congo.cs和ViewCart.aspx </ head >
 8 congo.aspx和congo.cs和ViewCart.aspx < body >
 9 congo.aspx和congo.cs和ViewCart.aspx     < form id = " form1 "  runat = " server " >
10 congo.aspx和congo.cs和ViewCart.aspx     < div >
11 congo.aspx和congo.cs和ViewCart.aspx     < table width = " 100% "  bgcolor = " teal " >
12 congo.aspx和congo.cs和ViewCart.aspx     < tr >
13 congo.aspx和congo.cs和ViewCart.aspx     < td >
14 congo.aspx和congo.cs和ViewCart.aspx     < asp:Button ID = " lblReturnShop "  Text = " Return to Shopping "  OnClick = " OnShop "  runat = " server "   />
15 congo.aspx和congo.cs和ViewCart.aspx    
16 congo.aspx和congo.cs和ViewCart.aspx     </ td >
17 congo.aspx和congo.cs和ViewCart.aspx     </ tr >
18 congo.aspx和congo.cs和ViewCart.aspx     </ table >
19 congo.aspx和congo.cs和ViewCart.aspx     </ div >
20 congo.aspx和congo.cs和ViewCart.aspx    
21 congo.aspx和congo.cs和ViewCart.aspx     < br  />
22 congo.aspx和congo.cs和ViewCart.aspx     < center >
23 congo.aspx和congo.cs和ViewCart.aspx     < asp:DataGrid ID = " MyDataGrid "  AutoGenerateColumns = " false "  CellPadding = " 2 "
24 congo.aspx和congo.cs和ViewCart.aspx      BorderWidth = " 1 "  BorderColor = " lightgray "  Font - Names = " Verdana "
25 congo.aspx和congo.cs和ViewCart.aspx      Font - Size = " 8pt "  GridLines = " vertical "  Width = " 90% "  OnItemCommand = " OnItemCommand "  runat = " server " >
26 congo.aspx和congo.cs和ViewCart.aspx       < Columns >
27 congo.aspx和congo.cs和ViewCart.aspx       < asp:BoundColumn HeaderText = " Item ID "  DataField = " ItemID " >
28 congo.aspx和congo.cs和ViewCart.aspx       </ asp:BoundColumn >
29 congo.aspx和congo.cs和ViewCart.aspx      
30 congo.aspx和congo.cs和ViewCart.aspx       < asp:BoundColumn HeaderText = " Title "  DataField = " Title " >
31 congo.aspx和congo.cs和ViewCart.aspx       </ asp:BoundColumn >
32 congo.aspx和congo.cs和ViewCart.aspx       < asp:BoundColumn HeaderText = " Price "  DataField = " Price "  DataFormatString = " {0:C} "
33 congo.aspx和congo.cs和ViewCart.aspx       HeaderStyle - HorizontalAlign = " center "  ItemStyle - HorizontalAlign = " right " >
34 congo.aspx和congo.cs和ViewCart.aspx        </ asp:BoundColumn >
35 congo.aspx和congo.cs和ViewCart.aspx       
36 congo.aspx和congo.cs和ViewCart.aspx        < asp:BoundColumn HeaderText = " Quantity "  DataField = " Quantity "  
37 congo.aspx和congo.cs和ViewCart.aspx        HeaderStyle - HorizontalAlign = " center "  ItemStyle - HorizontalAlign = " center " >
38 congo.aspx和congo.cs和ViewCart.aspx        </ asp:BoundColumn >
39 congo.aspx和congo.cs和ViewCart.aspx        < asp:ButtonColumn HeaderText = " Action "  Text = " Remove "
40 congo.aspx和congo.cs和ViewCart.aspx        HeaderStyle - HorizontalAlign = " center "  ItemStyle - HorizontalAlign = " center "  CommandName = " RemoveFromCart " >
41 congo.aspx和congo.cs和ViewCart.aspx         </ asp:ButtonColumn >
42 congo.aspx和congo.cs和ViewCart.aspx       </ Columns >
43 congo.aspx和congo.cs和ViewCart.aspx      
44 congo.aspx和congo.cs和ViewCart.aspx       < HeaderStyle BackColor = " teal "  ForeColor = " white "  Font - Bold = " true "   />
45 congo.aspx和congo.cs和ViewCart.aspx       < ItemStyle BackColor = " white "  ForeColor = " darkblue "   />
46 congo.aspx和congo.cs和ViewCart.aspx       < AlternatingItemStyle BackColor = " beige "  ForeColor = " darkBlue "   />
47 congo.aspx和congo.cs和ViewCart.aspx       </ asp:DataGrid >
48 congo.aspx和congo.cs和ViewCart.aspx     </ center >
49 congo.aspx和congo.cs和ViewCart.aspx     < h3 >< asp:Label ID = " Total "  runat = " server " ></ asp:Label ></ h3 >
50 congo.aspx和congo.cs和ViewCart.aspx     </ form >
51 congo.aspx和congo.cs和ViewCart.aspx </ body >
52 congo.aspx和congo.cs和ViewCart.aspx </ html >
53 congo.aspx和congo.cs和ViewCart.aspx
ViewCart.aspx.cs:
congo.aspx和congo.cs和ViewCart.aspx viewCart.cs

你可能感兴趣的:(view)