ShoppingCart
2019-04-26 08:22:09来源:博客园 阅读 ()
数据库设计
表结构
[dbo].[AdminInfo] AdminID, AdminName, AdminPassword, RoleID [dbo].[BK_Car] ID, CarID, ISBN, BookName, Price, Num [dbo].[BookInfo] ISBN, BookName, Author, Price, InCount, OutCount, Intro, Sort, ImageAds [dbo].[Indent] IndentID, UserName, BookName, Price, Num, IndentTime, FaHuoWay, PayHuoWay, FaHuo [dbo].[UserInfo] UserID, UserName, UserPwd, Email
连接字符串:
<connectionStrings> <add name="connStr" connectionString="Data Source=WINDOWS-AU6K8R5\SQLEXPRESS;Initial Catalog=BookShopping;User ID=sa;Pwd=123456" providerName="System.Data.SqlClient" /> </connectionStrings>
Model层SqlHelper封装
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; namespace ShoppingCartDBHelper { public class SqlHelper { public static readonly string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; public static DataTable GetDataTable(string sql,CommandType type,params SqlParameter[]pars) { using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlDataAdapter apter = new SqlDataAdapter(sql, conn)) { if (pars != null) { apter.SelectCommand.Parameters.AddRange(pars); } apter.SelectCommand.CommandType = type; DataTable da = new DataTable(); apter.Fill(da); return da; } } } ? public static int ExecuteNonquery(string sql, CommandType type, params SqlParameter[] pars) { using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(sql, conn)) { if (pars != null) { cmd.Parameters.AddRange(pars); } cmd.CommandType = type; conn.Open(); return cmd.ExecuteNonQuery(); } } } ? public DataTable FillDataTable(string v) { DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(connStr)) { SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = v; SqlDataAdapter ada = new SqlDataAdapter(); ada.SelectCommand = cmd; ada.Fill(dt); } return dt; } ? public static DataSet GetDataSet(String sql) { SqlConnection conn=new SqlConnection(connStr); SqlDataAdapter adapter=new SqlDataAdapter(sql,connStr); DataSet ds=new DataSet(); adapter.Fill(ds); return ds; } } }
?
母版页设计(仿豆瓣)
?
<!DOCTYPE html> ? <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>豆瓣书城</title> <asp:ContentPlaceHolder ID="head" runat="server"> </asp:ContentPlaceHolder> <link href="CSS/MasterStyle.css" rel="stylesheet" /> </head> <body> <form id="form1" runat="server"> <div class="top"> <span class="l"> <a href="#" target="_blank">豆瓣</a> | <a href="#" target="_blank">读书</a> | <a href="#" target="_blank">电影</a> | <a href="#" target="_blank">音乐</a> | <a href="#" target="_blank">同城</a> | <a href="#" target="_blank">小组</a> | <a href="#">阅读</a> <a href="#" target="_blank">FM</a> | <a href="#" target="_blank">时间</a> | <a href="#">豆品</a> <a href="#">更多</a> </span> <span class="r"> <a>下载豆瓣客户端</a> <a href="../Member/Login.aspx">登录</a><a href="">/注册</a> </span> ? </div> <div style="width: inherit; text-align: left; background-color: #F6F6F1; border-bottom: cadetblue;"> <div id="search"> <img src="Images/LOGO.png" /> <div style="height: 50px; width: 600px; margin: 15px 400px auto 50px;float: right"> <div id="left"> <asp:TextBox ID="TextBox1" runat="server" CssClass="search">书名、作者、ISBN</asp:TextBox> </div> <div id="right"> <asp:Image ID="Image1" runat="server" ImageUrl="Images/search.png" CssClass="searchbtn" /> </div> </div> </div> </div> <div id="main"> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </div> <div id="footer"> <span id="fl">© 2005-2019 douban.com, all rights reserved 北京豆网科技有限公司 </span> <span id="fr"> <a href="#">关于豆瓣</a> <a href="#">·在豆瓣工作</a> <a href="#">·联系我们</a> <a href="#">·免责说明</a> <a href="#">·帮助中心</a> <a href="#">·图书馆合作</a> <a href="#">·移动应用</a> <a href="#">·豆瓣广告</a> </span> </div> </form> </body> </html>
?
body { padding: 0; margin: 0; } .top { background-color: #545652; width: initial; height: 25px; font-size: 12px; padding-top: 10px; } .l a{ color: white; border:0; text-decoration: none; } .l{ float: left;} .r { float: right; } .search { width: 463px; height: 35px; border: 0; border-radius: 4px 0px 0px 4px; box-shadow: 2px 2px 2px #cdcdcd; vertical-align: middle; padding: 0px; } .searchbtn { padding: 0px; height: 35px; width: 35px; border-radius: 0px 4px 4px 0px; } #left { float: left; } #right { float: left; } #footer { height: 30px; width: inherit; background-color: #545652; } #fl { float: left; padding-top: 10px; } #fr { padding-top: 10px; float: right; ? } #fr a { text-decoration: none; font-size: 12px; color: white; }
百度识图作图床有点糊啊!
表现层商品展示页
<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="ShoppingCart.Index" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <br/><br/><br/><br/><br/> <a href="ShoppingCart.aspx">我的购物车</a> <br/><br/> <asp:DataList ID="DataList1" runat="server" Width="100%" RepeatDirection="Horizontal" OnItemDataBound="DataList1_OnItemDataBound" > <ItemTemplate> <%-- [ISBN],[BookName],[Author],[Price],[ImageAds] --%> <%-- ImageUrl ='<%# Eval("ImageAds") %>' --%> <div> <asp:Image ID="Image1" runat="server" ImageUrl ='<%# Eval("ImageAds") %>' Height="115" Width="105" /> </div> <div> <a href="ShoppingCart.aspx?ISBN=<%#Eval("ISBN") %>"><%# Eval("BookName")%></a> </div> <div> <a href="ShoppingCart.aspx?ISBN=<%#Eval("ISBN") %>"><img src="Images/add.jpg" alt="添加到购物车" border="0" title="添加到购物车" /> </a> </div> <div> <font color="red"><%# Eval("Price")%></font> </div> </ItemTemplate> <SelectedItemStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> </asp:DataList><br/><br/><br/><br/> <div style="float: right"> <asp:Label ID="lblCurrentPage" runat="server"></asp:Label>/ <asp:Label ID="lblPageCount" runat="server"></asp:Label>页 <asp:HyperLink ID="lnkFirst" runat="server">首页</asp:HyperLink> <asp:HyperLink ID="lnkPrev" runat="server">上页</asp:HyperLink> <asp:HyperLink ID="lnkNext" runat="server">下页</asp:HyperLink> <asp:HyperLink ID="lnkLast" runat="server">末页</asp:HyperLink> </div> <br/><br/> </asp:Content> ?
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.Bindlist(); } } ? private void Bindlist() { DbHelper helper = new DbHelper(); PagedDataSource pdSource = new PagedDataSource(); pdSource.DataSource = helper.FillTable("select [ISBN],[BookName],[Author],[Price],[ImageAds] from BookInfo") .DefaultView; pdSource.AllowPaging = true; DataList1.DataSource = pdSource; DataList1.DataBind(); } ? protected void DataList1_OnItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView drView = (DataRowView)e.Item.DataItem; // ((Image) e.Item.FindControl("Image1")).ImageUrl =drView["ImageAds"].ToString(); // string rootdir = Server.MapPath(HttpContext.Current.Request.ApplicationPath); // string path = drView["ImageAds"].ToString().Replace(rootdir, ""); //path = path.Replace(@"\",@"/"); // Response.Write("<script>alert('"+path+"');</script>"); // ((Image) e.Item.FindControl("Image1")).ImageUrl = Server.MapPath(path); //前面从数据库取同ImageUrl ='<%# Eval("ImageAds") %>' } }
这里使用了datalist模板列。通过地址栏传参给购物车页。
表现层购物车
<asp:GridView ID="gvCart" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="ID" ShowFooter="True" OnRowDataBound="gvCart_OnRowDataBound" OnRowDeleting="gvCart_OnRowDeleting" Height="331px"> <Columns> <asp:BoundField HeaderText="ISBN" DataField="ISBN" /> <asp:BoundField HeaderText="书名" DataField="BookName" /> <asp:TemplateField HeaderText="单价"> <ItemTemplate> <%# Eval("Price") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="数量"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_OnClick">-</asp:LinkButton> <asp:TextBox ID="txtCount" runat="server" Width="80" Height="16" Text='<%#Eval("Num") %>' ></asp:TextBox> <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_OnClick">+</asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="总价"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <%-- <% --%> <%-- --%> <%-- price = Convert.ToInt32(FindControl("txtCount").ClientID) * Convert.ToInt32(Eval("Price")); --%> <%-- Response.Write(price); --%> <%-- %> --%> </ItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="删除" DeleteText="删除" ShowDeleteButton="True" /> </Columns> <EmptyDataTemplate> 您的购物车中没有任何商品。 </EmptyDataTemplate> </asp:GridView>
后台代码:
public partial class ShoppingCart : System.Web.UI.Page { public static int price { get; set; } protected static PagedDataSource ps=new PagedDataSource(); public void Bind() { string sql = "select * from BK_Car"; DbHelper helper=new DbHelper(); ps.DataSource = helper.FillTable(sql).DefaultView; ps.AllowPaging = true; this.gvCart.DataSource = ps; this.gvCart.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindCartList(); } } private void BindCartList() { string isbn = Request.QueryString["ISBN"]; string sql = "select count(*) from BK_Car where ISBN="+isbn; // SqlParameter[] parameters = new SqlParameter[] // { // new SqlParameter("@ISBN",isbn), // }; DataSet dataSet = SqlHelper.GetDataSet(sql); if (dataSet.Tables[0].Rows[0][0].ToString() == "0") { string selsql = "select BookName,Price from BookInfo where ISBN="+isbn; //SqlParameter parameter = new SqlParameter("@ISBN", isbn); DataSet ds = SqlHelper.GetDataSet(selsql); string BookName = ds.Tables[0].Rows[0][0].ToString(); string Price = ds.Tables[0].Rows[0][1].ToString(); int num = 1; string insert = "insert into BK_Car(ISBN,BookName,Price,Num) values(@ISBN,@BookName,@Price,@Num)"; SqlParameter[] pars = new SqlParameter[] { new SqlParameter("@ISBN",isbn), new SqlParameter("@BookName",BookName), new SqlParameter("@Price",Price), new SqlParameter("@Num",num), }; SqlHelper.ExecuteNonquery(insert, CommandType.Text, pars); Bind(); } else { string update = "update BK_Car set Num=Num+1 where ISBN=@ISBN"; SqlParameter parameter = new SqlParameter("@ISBN", isbn); SqlHelper.ExecuteNonquery(update,CommandType.Text,parameter); Bind(); } } protected void gvCart_OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType==DataControlRowType.DataRow) { e.Row.Attributes.Add("onmouseover", "b=this.style.backgroundColor;this.style.backgroundColor='#E1ECEE'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=b"); TextBox tbBox = (TextBox) e.Row.FindControl("txtCount"); //处理问题:GridView模版列btn添加事件 //GridView,获取模版列txt的值 } } protected void gvCart_OnRowDeleting(object sender, GridViewDeleteEventArgs e) { //点击删除时从DataTable中删除对应的数据行 if (Session["Cart"] != null) { DataTable dt = (DataTable)Session["Cart"]; dt.Rows.RemoveAt(e.RowIndex); dt.AcceptChanges(); Session["Cart"] = dt; Response.Redirect("ShoppingCart.aspx"); } } }
代码待优化,非成品分层系统项目,后期override。
基操。
原文链接:https://www.cnblogs.com/fenqinearl/p/10759829.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:第3章 支持和规范
- HTML标签删除HTML示例代码 2020-03-08
- .NET 缓存模块设计实践 2020-01-20
- 详解asp.net mvc CodeFirst模式数据库迁移步骤 2019-12-14
- asp.net实现access数据库分页的方法 2019-12-13
- ASP.NET连接 Access数据库的几种方法 2019-11-29
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash