最简单的购物车(新手)
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
}