| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 889 人关注过本帖, 2 人收藏
标题:最简单的购物车(新手)
只看楼主 加入收藏
fishoftea
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-3-16
收藏(2)
 问题点数:0 回复次数:1 
最简单的购物车(新手)
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.Data.SqlClient;

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

    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
      
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        #region 按类别显示信息
        if ( == "Select")
        {
            Response.Redirect("Default.aspx?PetTypeID=" + DataList1.DataKeys[e.Item.ItemIndex]);
        }
        #endregion
    }
    protected void DataList2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        #region 详细信息
        if ( == "Details")
        {
            Response.Redirect("Details.aspx?PetID=" + DataList2.DataKeys[e.Item.ItemIndex]);
        }
        #endregion

        #region 购买
        if ( == "Buy")
        {
            #region 变量声明
                string id = this.DataList2.DataKeys[e.Item.ItemIndex].ToString();//商品编号
                string name = "";         //商品名称
                float price = 0.0f;       //商品单价
                int num = 0;              //商品数量
                float count = price * num;  //商品总价格
            #endregion

            #region 构造购物车临时表
                DataTable BusTable = new DataTable();
                BusTable.Columns.Add("ID", typeof(string));
                BusTable.Columns.Add("Name", typeof(string));
                BusTable.Columns.Add("Price", typeof(float));
                BusTable.Columns.Add("Num", typeof(int));
                BusTable.Columns.Add("Count", typeof(float));
            #endregion

            #region 变量赋值
                DataTable petdt = new DataTable();
                SqlConnection con = new SqlConnection();
                con.ConnectionString = ConfigurationManager.ConnectionStrings["WebShoppingConn"].ConnectionString;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                = "select * from pet where petid='" + id + "'";

                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(petdt);

                if (petdt.Rows.Count > 0)
                {
                    name = Convert.ToString(petdt.Rows[0]["PetName"]);
                    price = Convert.ToSingle(petdt.Rows[0]["PetPrice"]);

                }
                num = 1;
                count = price * num;
            #endregion

            #region 购物车的实现

            if (Session["Bus"] == null)//1.第一次购买
            {
                //Response.Write("你所购买的商品编号为" + DataList2.DataKeys[e.Item.ItemIndex]);
              
                DataRow dr = BusTable.NewRow();
                dr["ID"] = id;
                dr["Name"] = name;
                dr["Price"] = price;
                dr["Num"] = num;
                dr["Count"] = count;
                BusTable.Rows.Add(dr);

                Session["Bus"] = BusTable;
                //this.GridView1.DataSource = BusTable;
                //GridView1.DataBind();
            }
            else//2.第N次购买
            {
                DataTable busTable = (DataTable)Session["Bus"];
                DataRow[] dr = busTable.Select("ID='"+id+"'");
                if (dr.Length == 0)//1)购买新商品
                {
                    DataRow newdr = busTable.NewRow();
                    newdr["ID"] = id;
                    newdr["Name"] = name;
                    newdr["Price"] = price;
                    newdr["Num"] = num;
                    newdr["Count"] =count ;
                    busTable.Rows.Add(newdr);
                    
                }
                else             //2)购买已有商品
                {
                    foreach (DataRow tdr in busTable.Rows)
                    {
                        if (tdr["ID"].ToString() == id)
                        {
                            tdr["Num"] = Convert.ToInt32(tdr["Num"]) + 1;
                            tdr["Count"] = Convert.ToSingle(tdr["Price"]) * Convert.ToInt32(tdr["Num"]);
                        }
                    }
                    
                }
                Session["Bus"] = busTable;
            }
            #endregion
        }
        #endregion
    }
搜索更多相关主题的帖子: 购物车 
2008-04-09 15:05
c4905810
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-12-12
收藏
得分:0 
这个怎么样打开阿?
2009-11-19 21:27
快速回复:最简单的购物车(新手)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023519 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved