欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ShoppingCart

程序员文章站 2022-03-20 12:14:08
Model层SqlHelper封装 百度识图作图床有点糊啊! 后台代码: 这里使用了datalist模板列。通过地址栏传参给购物车页。 后台代码: 代码待优化,非成品分层系统项目,后期override。 基操。 ......

 

数据库设计
表结构
[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> |&nbsp;
                    <a href="#" target="_blank">读书</a>&nbsp; |
                    <a href="#" target="_blank">电影</a>&nbsp;|
                    <a href="#" target="_blank">音乐</a>&nbsp; |
                    <a href="#" target="_blank">同城</a>&nbsp; |
                    <a href="#" target="_blank">小组</a>&nbsp; |
                    <a href="#">阅读</a>
                <a href="#" target="_blank">fm</a>&nbsp; |
                    <a href="#" target="_blank">时间</a>&nbsp; |
                    <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;
}

ShoppingCart

百度识图作图床有点糊啊!

表现层商品展示页
<%@ 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>页 &nbsp;
        <asp:hyperlink id="lnkfirst" runat="server">首页</asp:hyperlink>
        &nbsp;
        <asp:hyperlink id="lnkprev" runat="server">上页</asp:hyperlink>
        &nbsp;
        <asp:hyperlink id="lnknext" runat="server">下页</asp:hyperlink>
        &nbsp;
        <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");
            }
        }
    }

ShoppingCart

代码待优化,非成品分层系统项目,后期override。

基操。