ASP.NET购物车实现过程详解
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
public partial class shopcart : System.Web.UI.Page
{
Hashtable ht;
DataTable dt;
string connstr = "Data Source=.SQLEXPRESS;AttachDbFilename=F:
est.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection conn;
SqlCommand cmd;
SqlDataReader sdr;
protected void Page_Load(object sender, EventArgs e)
{
dt = new DataTable();
DataColumn col = new DataColumn();
col.ColumnName = "id";
col.DataType = System.Type.GetType("System.String");
dt.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "name";
col.DataType = System.Type.GetType("System.String");
dt.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "Num";
col.DataType = System.Type.GetType("System.Int32");
dt.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "price";
col.DataType = System.Type.GetType("System.Single");
dt.Columns.Add(col);
col = new DataColumn();
col.ColumnName = "Total";
col.DataType = System.Type.GetType("System.Single");
dt.Columns.Add(col);
if (!IsPostBack)
{
Bind();
}
}
public void Bind()
{
if (Session["shopcar"] == null)
{
Response.Write("");
}
else
{
ht = (Hashtable)Session["shopcar"];
foreach (object item in ht.Keys)
{
string id = item.ToString();
int num = int.Parse((ht[item].ToString()));
string sql = "select book_name,price from book_info where book_id='" + id + "'";
conn = new SqlConnection(connstr);
cmd = new SqlCommand(sql, conn);
conn.Open();
sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
sdr.Read();
DataRow row = dt.NewRow();
row["id"] = id;
row["Num"] = num;
row["name"] = sdr.GetString(0);
row["price"] = float.Parse(sdr[1].ToString());
row["total"] = num * (float.Parse(sdr[1].ToString()));
dt.Rows.Add(row);
}
sdr.Close();
conn.Close();
}
}
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
protected void textbox1_TextChanged(object sender, EventArgs e)
{
Hashtable ht = (Hashtable)Session["shopcar"];
if (ht == null) return;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string id = GridView1.Rows[i].Cells[0].Text.ToString();
Response.Write(id);
string num = ((TextBox)GridView1.Rows[i].FindControl("textbox1")).Text;
Response.Write(" " + num + "
");
ht[id] = num;
}
Session["shopcar"] = ht;
Bind();
}
protected void button1_Click(object sender, EventArgs e)
{
string id = ((Button)sender).CommandArgument;
Hashtable ht = (Hashtable)Session["shopcar"];
if (ht == null) return;
ht.Remove(id);
Bind();
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播