购物清单页面的处理shoppingCart.aspx and shoppingCart.aspx.cs

请输入大于10个字符的资源描述 
<% @ Register TagPrefix = " uc1 "  TagName = " Footer "  Src = " UserControls/Footer.ascx "   %>
<% @ Register TagPrefix = " uc1 "  TagName = " Header "  Src = " UserControls/Header.ascx "   %>
<% @ Page language = " c# "  Inherits = " ASPNETSZ4.ShoppingCart "  CodeFile = " ShoppingCart.aspx.cs "   %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.0 Transitional//EN "   >
< HTML >
    
< HEAD >
        
< title >
            
<%   =  pub.sTitle  %>
        
</ title >
        
< meta name = " GENERATOR "  Content = " Microsoft Visual Studio .NET 7.1 " >
        
< meta name = " CODE_LANGUAGE "  Content = " C# " >
        
< meta name = " vs_defaultClientScript "  content = " JavaScript " >
        
< meta name = " vs_targetSchema "  content = " http://schemas.microsoft.com/intellisense/ie5 " >
        
< LINK href = " Style.css "  type = " text/css "  rel = " stylesheet " >
    
</ HEAD >
    
< body >
        
< form id = " Form1 "  method = " post "  runat = " server " >
            
< uc1:header id = " Header1 "  runat = " server " ></ uc1:header >
            
< TABLE cellSpacing = " 0 "  cellPadding = " 0 "  width = " 100% "  align = " center "  border = " 0 " >
                
< TR >
                    
< TD >
                        
< TABLE cellSpacing = " 0 "  cellPadding = " 0 "  width = " 100% "  border = " 0 " >
                            
< TR >
                                
< TD vAlign = " top "  width = " 200 " ></ TD >
                                
< td width = " 2 " ></ td >
                                
< TD vAlign = " top "  align = " middle " >
                                    
< table cellSpacing = " 0 "  cellPadding = " 0 "  width = " 98% " >
                                        
< tr >
                                            
< td background = " Images/BannerBGOrange.gif "  height = " 26 " >& nbsp; & nbsp;购物清单列表 </ td >
                                            
< td background = " Images/BannerBGOrange.gif "  align = " right " >< asp:label id = " lbMsg "  runat = " server "  ForeColor = " Red " ></ asp:label ></ td >
                                        
</ tr >
                                    
</ table >
                                    
< asp:Panel id = " Panel1 "  runat = " server " >
                                        
< asp:datagrid id = " DataGrid1 "  runat = " server "  DataKeyField = " ProductID "  AutoGenerateColumns = " False "  Width = " 98% "  BorderColor = " #CC9966 "  BorderStyle = " None "  BorderWidth = " 1px "  BackColor = " White "  CellPadding = " 4 " >
                                            
< SelectedItemStyle Font - Bold = " True "  ForeColor = " #663399 "  BackColor = " #FFCC66 " ></ SelectedItemStyle >
                                            
< AlternatingItemStyle BackColor = " Cornsilk " ></ AlternatingItemStyle >
                                            
< ItemStyle ForeColor = " #330099 "  BackColor = " White " ></ ItemStyle >
                                            
< HeaderStyle Font - Bold = " True "  HorizontalAlign = " Center "  ForeColor = " #FFFFCC "  BackColor = " #990000 " ></ HeaderStyle >
                                            
< FooterStyle ForeColor = " #330099 "  BackColor = " #FFFFCC " ></ FooterStyle >
                                            
< Columns >
                                                
< asp:TemplateColumn Visible = " False "  HeaderText = " 货号 " >
                                                    
< ItemTemplate >
                                                        
< asp:Label id = lbProductID runat = " server "  Text = ' <%# DataBinder.Eval(Container.DataItem, "ProductID") %> '  Visible = " false " >
                                                        
</ asp:Label >
                                                    
</ ItemTemplate >
                                                
</ asp:TemplateColumn >
                                                
< asp:BoundColumn DataField = " ModelNumber "  HeaderText = " 规格型号 " ></ asp:BoundColumn >
                                                
< asp:BoundColumn DataField = " ModelName "  HeaderText = " 商品名称 " ></ asp:BoundColumn >
                                                
< asp:TemplateColumn HeaderText = " 数量 " >
                                                    
< ItemTemplate >
                                                        
< asp:TextBox id = txtQuantity runat = " server "  BorderStyle = " Dotted "  BorderWidth = " 1px "  Text = ' <%# DataBinder.Eval(Container.DataItem, "Quantity") %> '  ToolTip = " 双击后直接修改数量 "  Columns = " 4 "  MaxLength = " 3 "  width = " 100% " >
                                                        
</ asp:TextBox >
                                                    
</ ItemTemplate >
                                                
</ asp:TemplateColumn >
                                                
< asp:BoundColumn DataField = " UnitCost "  HeaderText = " 单价 "  DataFormatString = " {0:c} " >
                                                    
< ItemStyle HorizontalAlign = " Right " ></ ItemStyle >
                                                
</ asp:BoundColumn >
                                                
< asp:BoundColumn DataField = " ExtendedAmount "  HeaderText = " 小计 "  DataFormatString = " {0:c} " >
                                                    
< ItemStyle HorizontalAlign = " Right " ></ ItemStyle >
                                                
</ asp:BoundColumn >
                                                
< asp:TemplateColumn HeaderText = " " >
                                                    
< ItemTemplate >
                                                        
< DIV align = " center " >
                                                            
< asp:CheckBox id = " cbRemove "  runat = " server " ></ asp:CheckBox ></ DIV >
                                                    
</ ItemTemplate >
                                                
</ asp:TemplateColumn >
                                            
</ Columns >
                                            
< PagerStyle HorizontalAlign = " Center "  ForeColor = " #330099 "  BackColor = " #FFFFCC " ></ PagerStyle >
                                        
</ asp:datagrid >
                                        
< TABLE cellSpacing = " 0 "  cellPadding = " 0 "  width = " 98% " >
                                            
< TR >
                                                
< TD align = " middle "  background = " Images/BannerBGOrange.gif "  height = " 26 " >
                                                    
< asp:Button id = " Button1 "  runat = " server "  BorderStyle = " None "  BorderWidth = " 1px "  BackColor = " Transparent "  Text = " √全部选中 "  onclick = " Button1_Click " ></ asp:Button ></ TD >
                                                
< TD align = " middle "  background = " Images/BannerBGOrange.gif "  height = " 26 " >
                                                    
< asp:Button id = " Button2 "  runat = " server "  BorderStyle = " None "  BorderWidth = " 1px "  BackColor = " Transparent "  Text = " □全部取消 "  onclick = " Button2_Click " ></ asp:Button ></ TD >
                                                
< TD align = " right "  background = " Images/BannerBGOrange.gif "  height = " 26 " > 合计金额:
                                                
</ TD >
                                                
< TD align = " right "  background = " Images/BannerBGOrange.gif "  height = " 26 " >
                                                    
< asp:label id = " lbTotal "  runat = " server "  EnableViewState = " false " ></ asp:label ></ TD >
                                            
</ TR >
                                        
</ TABLE >
                                        
< BR >
                                        
< TABLE cellSpacing = " 0 "  cellPadding = " 0 "  align = " center "  border = " 0 " >
                                            
< TR >
                                                
< TD align = " middle " >
                                                    
< asp:ImageButton id = " ImageButton1 "  runat = " server "  ImageUrl = " Images/UpdateCart.gif " ></ asp:ImageButton ></ TD >
                                                
< TD align = " middle "  width = " 10 " ></ TD >
                                                
< TD align = " middle " >
                                                    
< asp:ImageButton id = " ImageButton2 "  runat = " server "  ImageUrl = " Images/CheckOut.gif " ></ asp:ImageButton ></ TD >
                                            
</ TR >
                                        
</ TABLE >
                                    
</ asp:Panel >
                                
</ TD >
                            
</ TR >
                        
</ TABLE >
                    
</ TD >
                
</ TR >
            
</ TABLE >
            
< uc1:footer id = " Footer1 "  runat = " server " ></ uc1:footer >
        
</ form >
    
</ body >
</ HTML >

 

 

using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Web;
using  System.Web.SessionState;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;

namespace  ASPNETSZ4
{
    
/// <summary>
    
/// Summary description for ShoppingCart.
    
/// </summary>

    public partial class ShoppingCart : System.Web.UI.Page
    
{
        
protected Pub pub = new Pub();
    
        
protected void Page_Load(object sender, System.EventArgs e)
        
{
            
            
//put user code to initialize the page here 
            if (Page.IsPostBack == false
            
{
                BindData();
            }

        }

        
/// <summary>
        
/// bind data 
        
/// </summary>

        void BindData() 
        
{
            String CartID 
= pub.GetShoppingCartID();
            DataGrid1.DataSource 
= pub.spShoppingCartList(CartID);
            DataGrid1.DataBind();
            DispInfo();
        }

        
/// <summary>
        
/// display info about shopping cartId
        
/// </summary>

        void DispInfo()
        
{
            
//当前用户的购物车ID
            String CartID = pub.GetShoppingCartID();
            
//lsMsg 用来显示购物车的信息
            if (DataGrid1.Items.Count == 0
            
{
                lbMsg.Text 
= pub.SiteName + " -- 购物清单 -- 暂时为空 -- 请继续购物 ";
                Panel1.Visible 
= false;
            }

            
else 
            
{
                lbMsg.Text 
= pub.SiteName + " -- 购物清单 -- 共有 " + DataGrid1.Items.Count.ToString() + " 个记录 -- 请继续购物 ";
                Panel1.Visible 
= true;
                
//<asp:BoundColumn DataField="ExtendedAmount" HeaderText="小计" DataFormatString="{0:c}">
                lbTotal.Text = String.Format( "{0:c}", pub.spShoppingCartTotal(CartID));
            }

        }

        
/// <summary>
        
/// update shoppingCart data
        
/// </summary>

        void ShoppingCartUpdate()
        
{
            String CartID 
= pub.GetShoppingCartID();
            
// 遍历 DataGird1 里的 txtQuantity 与 cbRemove 值
            for (int i=0; i<DataGrid1.Items.Count; i++
            
{
                
// 获取相关值
                
//CheckBoxList.FindControl 在当前的命名容器中搜索指定的服务器控件。 
                TextBox TxtQuantity = (TextBox) DataGrid1.Items[i].FindControl("txtQuantity");
                CheckBox cbRemove 
= (CheckBox) DataGrid1.Items[i].FindControl("cbRemove");
                
int iQty;
                
// 保护异常
                try 
                
{
                    iQty 
= Int32.Parse(TxtQuantity.Text);
                    
// 如果数量改变或者选中
                    if (iQty != (int)DataGrid1.DataKeys[i] || cbRemove.Checked == true
                    
{
                        Label lbProductID 
= (Label) DataGrid1.Items[i].FindControl("lbProductID");
                        
if (iQty == 0 || cbRemove.Checked == true
                        
{
                            pub.spShoppingCartRemoveItem(CartID, Int32.Parse(lbProductID.Text));
                        }

                        
else 
                        
{
                            pub.spShoppingCartUpdateQty(CartID, Int32.Parse(lbProductID.Text), iQty);
                        }

                    }

                }

                
catch 
                
{
                    lbMsg.Text 
= "系统提示: 您刚才至少有一处有输入错误.";
                }

            }

        }



        
Web Form Designer generated code
        
/// <summary>
        
/// update shopping Cart count 
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>


        
private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        
{
            ShoppingCartUpdate();
            BindData();
        }

/// <summary>
/// pay out money to shopping cart 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>

        private void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        
{
            ShoppingCartUpdate();
            
if (DataGrid1.Items.Count > 0)
                
// 付款结帐前, 再次检查是否非空购物车, 然后并没作过多的处理, 而是直接转向订单确认页面
                Response.Redirect("Checkout.aspx");
            
else
                lbMsg.Text 
= "系统提示: 您当前的购物清单是空的, 所以, 您还无需进行结帐付款.";
        }

        
/// <summary>
        
/// 购买 的商品全部选中然后计算总价值
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        protected void Button1_Click(object sender, System.EventArgs e)
        
{
            
for(int i=0; i<DataGrid1.Items.Count; i++)
            
{
                ((CheckBox)DataGrid1.Items[i].Cells[
0].FindControl("cbRemove")).Checked = true;
            }

            DispInfo();
        }

      
/// <summary>
        
/// 购买 的商品全部取消,然后检验购物车的总价数
      
/// </summary>
      
/// <param name="sender"></param>
      
/// <param name="e"></param>

        protected void Button2_Click(object sender, System.EventArgs e)
        
{
            
for(int i=0; i<DataGrid1.Items.Count; i++)
            
{
                ((CheckBox)DataGrid1.Items[i].Cells[
0].FindControl("cbRemove")).Checked = false;
            }

            DispInfo();
        }

    }

}

此购物清单的设计要点:

1.页面的加载为Page.isPostBack

2.DataBind method

3.购物车信息的显示函数

要注意这种写法:

if (DataGrid1.Items.Count == 0)
   {
    lbMsg.Text = pub.SiteName + " -- 购物清单 -- 暂时为空 -- 请继续购物 ";
    Panel1.Visible = false;

   }
   else
   {
    lbMsg.Text = pub.SiteName + " -- 购物清单 -- 共有 " + DataGrid1.Items.Count.ToString() + " 个记录 -- 请继续购物 ";
    Panel1.Visible = true;
                //<asp:BoundColumn DataField="ExtendedAmount" HeaderText="小计" DataFormatString="{0:c}">
    lbTotal.Text = String.Format( "{0:c}", pub.spShoppingCartTotal(CartID));
   }

4.更新购物车的数据。

 //CheckBoxList.FindControl 在当前的命名容器中搜索指定的服务器控件。
    TextBox TxtQuantity = (TextBox) DataGrid1.Items[i].FindControl("txtQuantity");
    CheckBox cbRemove = (CheckBox) DataGrid1.Items[i].FindControl("cbRemove");

 

5.更新购物车中商品的数量和总钱数,此处是用二个Image button  来实现的。具体参照上面的代码

6.对选中商品和清空商品的处理

就是二个BUTTON的事件程序

雅琦

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值