一个简单的购物车供初学者学习

源文地址:http://www.cnblogs.com/mdy41034264/archive/2009/02/12/1389522.html

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
/// <summary>
/// Shopping 的摘要说明
/// </summary>
public class Shopping
{
    
//购物车
    public Hashtable SelectedGoods = new Hashtable();
    
public Shopping()
    {

    }
}

 

 


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    
private Shopping obj;
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (Session["goods"]==null)
        {
            obj 
= new Shopping();
            Session[
"goods"= obj;
        }
        
else
        {
            obj 
= Session["goods"as Shopping;
        }
        
if (!IsPostBack)
        {
            ShowSelectedGoods();
//显示被选中的商品
        }
    }
    
protected void btnShoppingGoods_Click(object sender, EventArgs e)
    {
        SaveSelectedGoods();
//保存被选中的商品
        Response.Redirect("ShowGoods.aspx");

    }
    
protected void btnNextBuyComputer_Click(object sender, EventArgs e)
    {
        SaveSelectedGoods();
        Response.Redirect(
"BuyComputer.aspx");
    }

    
private void SaveSelectedGoods()
    {
        CheckBox chk;
        
//循环检查位于窗体上的所有控件
        foreach (Control ctl in form1.Controls)
        {
            
//如果控件类型名为“CheckBox”
            if (ctl.GetType().Name == "CheckBox")
            {
                chk 
= ctl as CheckBox;
                
//检查一下购物车中是否已有此书了
                if (obj.SelectedGoods.ContainsKey("book:" + chk.ID))
                {
                    
if (chk.Checked == false)
                        
//从购物车中移除此书
                        obj.SelectedGoods.Remove("book:" + chk.ID);
                }
                
else  //购物车中没有此书
                    if (chk.Checked)  //复选框选中
                        
//向购物车中添加此书
                        obj.SelectedGoods.Add("book:" + chk.ID, chk.Text);
            }
        }
        
//将购物车对象保存到session中
        Session["goods"= obj;
    }

    
private void ShowSelectedGoods()
    {
        
foreach (Control ctl in form1.Controls)
            
if (ctl.GetType().Name == "CheckBox")
                (ctl 
as CheckBox).Checked = false;


        
foreach (Object key in obj.SelectedGoods.Keys)
        {
            
if (key.ToString().IndexOf("book:"!= -1)
            {
                
string id = key.ToString().Substring(key.ToString().LastIndexOf(":"+ 1);
                Control ctl 
= form1.FindControl(id);
                
if (ctl != null)
                    (ctl 
as CheckBox).Checked = true;
            }
        }
    }
}

 

 


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BuyComputer : System.Web.UI.Page
{
    
private Shopping obj;
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (Session["goods"== null)
        {
            obj 
= new Shopping();
            Session[
"goods"= obj;
        }
        
else
        {
            obj 
= Session["goods"as Shopping;
        }
        
if (!IsPostBack)
        {
            ShowSelectedArticles();
        }
    }
    
protected void btnBuyBook_Click(object sender, EventArgs e)
    {
        SaveSelectedArticles();
        Response.Redirect(
"BuyBook.aspx");
    }

    
private void SaveSelectedArticles()
    {
        CheckBox chk;
        
//循环检查位于窗体上的所有控件
        foreach (Control ctl in form1.Controls)
        {
            
//如果控件类型名为“CheckBox”
            if (ctl.GetType().Name == "CheckBox")
            {
                chk 
= ctl as CheckBox;
                
//检查一下购物车中是否已有此电脑商品了
                if (obj.SelectedGoods.ContainsKey("computer:" + chk.ID))
                {
                    
if (chk.Checked == false)
                        
//从购物车中移除此电脑商品
                        obj.SelectedGoods.Remove("computer:" + chk.ID);
                }
                
else  //购物车中没有此电脑商品
                    if (chk.Checked)  //复选框选中
                        
//向购物车中添加此电脑商品
                        obj.SelectedGoods.Add("computer:" + chk.ID, chk.Text);
            }
        }
        
//将购物车对象保存到session中
        Session["articles"= obj;
    }

    
private void ShowSelectedArticles()
    {
        
foreach (Control ctl in Controls)
            
if (ctl.GetType().Name == "CheckBox")
                (ctl 
as CheckBox).Checked = false;


        
foreach (Object key in obj.SelectedGoods.Keys)
        {
            
if (key.ToString().IndexOf("computer:"!= -1)
            {
                
string id = key.ToString().Substring(key.ToString().LastIndexOf(":"+ 1);
                Control ctl 
= form1.FindControl(id);
                
if (ctl != null)
                    (ctl 
as CheckBox).Checked = true;
            }

        }
    }
    
protected void btnShoppingGoods_Click(object sender, EventArgs e)
    {
        SaveSelectedArticles();
        Response.Redirect(
"ShowGoods.aspx");
    }
}

 

 


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ShowGoods : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (Session["goods"]==null)
        {
            Label lblinfo 
= new Label();
            lblinfo.Text 
= "";
            PlaceHolder1.Controls.Add(lblinfo);
        }
        
else
        {
            Shopping shop 
= Session["goods"as Shopping;
            
if (shop.SelectedGoods.Count==0)
            {
                Label lblinfo 
= new Label();
                lblinfo.Text 
= "";
                PlaceHolder1.Controls.Add(lblinfo);
            }
            
else
            {
                BulletedList list 
= new BulletedList();
                
foreach (object key in shop.SelectedGoods.Keys)
                {
                    list.Items.Add(shop.SelectedGoods[key].ToString());
                }
                PlaceHolder1.Controls.Add(list);
            }
        }
    }
    
protected void btnBuyBook_Click(object sender, EventArgs e)
    {
        Response.Redirect(
"BuyBook.aspx");
    }
    
protected void btnBuyComputer_Click(object sender, EventArgs e)
    {
        Response.Redirect(
"BuyComputer.aspx");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值