ASP.NET购物车实现过程详解
本文实例为大家分享了ASP.NET实现购物车的具体代码,供大家参考,具体内容如下
1、 将test数据库附加到数据库管理系统中;数据库中的book_info包含下列数据:
2、 新建一个网站,将images文件夹复制到网站中;
3、 在Default.aspx中,通过DataList控件展示数据库中的所有数据,以行为主序,每行3列,单击购买按钮时,将商品的ID和数量保存到HashTable中,并将HashTable放置到Session中。
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { string id = e.CommandArgument.ToString(); Hashtable ht; if (Session["shopcar"] == null) { ht = new Hashtable(); ht.Add(id, 1); Session["shopcar"] = ht; } else { ht = (Hashtable)Session["shopcar"]; if (ht.Contains(id)) { int count = int.Parse(ht[id].ToString()); ht[id] = count + 1; Session["shopcar"] = ht; Response.Write(count + 1); } else { ht.Add(id, 1); Session["shopcar"] = ht; } } }
4、 在Default.aspx中添加一个超链接,链接到shopcart.aspx,在shopcart.aspx中显示用户购买的商品信息。
提示:
A、在shopcart中先定义下列变量:
Hashtable ht; DataTable dt; string connstring=@"DataSource=.\SQLEXPRESS;Initial Catalog=test;Integrated Security=True"; SqlConnection conn; SqlCommand cmd; SqlDataReader sdr;
B、页面中添加一个GridView。
C、在page_load中,将dt实例化,建立各列。
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("<script>if(confirm('你没有登录')window.location='Default15.aspx';else window.close();</script>"); } else { ht = (Hashtable)Session["shopcar"]; foreach (object item in ht.Keys) { string id = item.ToString(); int num = int.Parse(ht[item].ToString()); string sql = "selectbook_name,price from book_info where book_id='" + id + "'"; conn = new SqlConnection(connstring); 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(); } }
D、这时可以看到用户购买的商品,但不能修改数量,也不能删除。
E、添加修改数量,删除商品功能,在aspx页面中定义GridView中的各列:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="id" HeaderText="ID" /> <asp:BoundField DataField="name" HeaderText="名称" /> <asp:BoundField DataField="price" HeaderText="价格" /> <asp:TemplateField> <ItemTemplate> <asp:TextBox runat="server" ID="textbox1" Text='<%# Eval("Num") %>' ontextchanged="textbox1_TextChanged" AutoPostBack="True" ></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="total" HeaderText="总计" /> <asp:TemplateField> <ItemTemplate> <asp:Button runat="server" ID="button1" CommandArgument='<%# Eval("id") %>' Text="删除" onclick="button1_Click" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
F、为GridView中的文本框添加TextChanged事件:
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+"<br />"); ht[id] = num; } Session["shopcar"] = ht; Bind(); }
G、为按钮添加单击事件:
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(); }
购物车代码:showcart.aspx.cs
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: \\test.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("<script>if(confirm('你没有登录')window.location='Default.aspx';else window.close();</script>"); } 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 + "<br />"); 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(); } }
制作一个简单的购物车就是这么简单,大家可以按照我的思路进行创作,在此基础上在添加一些功能。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
- 在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
ASP.NET Core根据环境变量支持多个 appsettings.json配置文件
这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- 这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
- 这篇文章主要为大家详细介绍了JS实现购物车中商品总价的计算 ,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-03-07
- 这篇文章主要为大家详细介绍了React列表栏及购物车组件使用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-06-28
详解ASP.NET Core 中基于工厂的中间件激活的实现方法
这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22asp.net通过消息队列处理高并发请求(以抢小米手机为例)
这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22ASP.NET单选按钮控件RadioButton常用属性和方法介绍
RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22ASP.NET 2.0中的数据操作:使用两个DropDownList过滤的主/从报表
在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有...2016-05-19- 这篇文章主要为大家详细介绍了vue实现简单购物车案例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-06-01
ASP.NET中iframe框架点击左边页面链接 右边显示链接页面内容
这篇文章主要介绍了ASP.NET中iframe框架点击左边页面链接,右边显示链接页面内容的实现代码,感兴趣的小伙伴们可以参考一下...2021-09-22- ASP.NET Web API具有与ASP.NET MVC类似的编程方式,ASP.NET Web API不仅仅具有一个完全独立的消息处理管道,而且这个管道比为ASP.NET MVC设计的管道更为复杂,功能也更为强大。下面创建一个简单的Web API项目,需要的朋友可以参考下...2021-09-22
- 在购物网站中,加入购物车的功能是必须的功能,有的网站在用户点击加入购物车按钮时,就会出现该商品从点击出以抛物线的动画相似加入购物车,这个功能看起来非常炫,对用户体验也有一定的提高。下面介绍基于jquery fly插件实现加入购物车抛物线动画效果...2016-04-06
- 这篇文章主要介绍了ASP.NET连接MySql数据库的2个方法及示例,使用的是MySQL官方组件和ODBC.NET,需要的朋友可以参考下...2021-09-22
- HTML 首先载入jQuery库文件和jquery.fly.min.js插件。复制代码 代码如下: <script src="jquery.js"></script> <script src="jquery.fly.min.js"></script> 接着,将商品信息html结构布置好,本例中,我们用四个商品并排...2015-03-15
- 这篇文章主要介绍了Asp.Net使用Bulk实现批量插入数据的方法,对于进行asp.net数据库程序设计非常有借鉴价值,需要的朋友可以参考下...2021-09-22
在ASP.NET 2.0中操作数据之二十九:用DataList和Repeater来显示数据
本文主要讲解ASP.NET 2.0中如何使用DataList 和 Repeater 来呈现数据,DataList包含一个table标记,而Repeater不会添加任何额外的代码,个人在实际开发中更推荐使用Repeater。...2021-09-22- 这篇文章主要介绍了获取DataTable选择第一行某一列值,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了jQuery使用cookie与json简单实现购物车功能的方法,介绍了jQuery实现购物车的步骤与关键代码,需要的朋友可以参考下...2016-04-18