webform 购物车小程序,主要学习session的使用

实现一个简单的购物网站

一、考试时间:8小时

二、开发工具:VS2005+Sql2000

三、数据库:见附件

四、需要实现的页面:

Index.aspx:浏览商品页面,显示商品列表,用户可以点击“购买“。

ViewCart.aspx:查看购物车页面,显示已购买的商品信息,可以点击“删除“已买的商品

ViewAccount.aspx:查看个人账户余额

Login.aspx:登录页面

 

建议使用的技术:母版页、GridView、泛型集合、自定义实体类、自定义数据库访问类、其它常Web控件。

 

五、实现功能:

1、  显示商品列表
                       

2、  实现购买功能,购买的时候动态显示购物车中的商品数量和商品总价格

3、  点击查看购物车后,显示已购买的商品。注意“购买数量”列,如果对一种商品点击购买多次,其“购买数量”不断增加。

4、  删除购物车中已购买的商品。
如果某商品的“购买数量”为1时,则点击“删除”时,直接从购物车中删除该商品;
如果商品的“购买数量”大于1时,点击一次“删除”时,把其购买数量减1。直到该商品购买数量为1时,再点击删除时,删除该商品

5、  在查看完购物车后还可以点击“浏览商品”继续购买。并在上面显示已购买的商品数量和总价格。

6、  在“查看购物车“后,可以提交订单

但在提交订单时,须完成以下功能:

a)         检查用户是否已登录,未登录则转到Login.aspx页面

 

b)        检查用户账户余额是否能够满足本次够买

c)         检查库存数量是否满足本次够买

d)        如果以上条件都满足则

  1.                         i.              从用户账户中扣除本次购买的总价格
  2.                       ii.              从商品库存中扣除本次每种商品的购买数量
  3.                     iii.              向订单表和订单内容表中加入本次购买的商品信息

7、  点击查看账户,可以查看该用户的账户余额。

对数据库操作的方法代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// FruitBF 的摘要说明
/// </summary>
public class FruitBF
{
    private DataClassesDataContext _Context;
    public FruitBF()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
        _Context = new DataClassesDataContext();
    }

    public Fruit Select(string ids)
    {
        var query=_Context.Fruit.Where(p => p.Ids == ids);
        if (query.Count()>0)
        {
            return query.First();
        }
        return null;
    }

    public List<Fruit> Select()
    {
        return _Context.Fruit.ToList();
    }
    public void update(Fruit data)
    {
        Fruit newdata = _Context.Fruit.Where(p => p.Ids == data.Ids).First();

        newdata.Numbers = data.Numbers;
        _Context.SubmitChanges();
    }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// OrderDetailsBF 的摘要说明
/// </summary>
public partial class OrderDetails
{

    private DataClassesDataContext _Context = new DataClassesDataContext();
        public string FruitName
        {
            //相关子查询,用订单表里水果编号查询水果表里水果的名字
            get
            {
               var query = _Context.Fruit.Where(p => p.Ids == this.FruitCode);
                if (query.Count() > 0)
                {
                    return query.First().Name;
                }
                return "";
            }
        }
        public decimal FruitPrice
        {

            get
            {
                var query = _Context.Fruit.Where(p => p.Ids == this._FruitCode);
                if (query.Count() > 0)
                {
                    return query.First().Price.Value;
                }

                return 0;
            }

        }
        public List<OrderDetails> Select()
        {
            return _Context.OrderDetails.ToList();
        }
     
    }

主界面aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
        .tr1 {
            text-align:center;
            background-color:#0094ff;
            font-weight:bolder;
        }
        .tr2 {
            text-align:center;
            background-color:#ffd800;
        }
          .tr3 {
            text-align:center;
          
        }
        #id1 {
            
            top:0px;
            left:300px;
        }
          #id2 {
              position:absolute;
            top:165px;
            left:100px;
        }
      
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="ddd">
      <div id="id2" class="ddd">
          <p><a href="Default.aspx">浏览商品</a></p>
         <p> <a href="zhanghu.aspx">查看账户</a></p>
         <p> <a href="gouwuche.aspx">查看购物车</a></p>
          <p><a href="denglu.aspx">登录</a></p>
      </div>
    <div id="id1">

     
        <center><h1><font color="blue">大苹果购物网</font></h1></center>
           &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Literal ID="Literal1" runat="server"></asp:Literal>
        <br />

        <asp:Literal ID="Literal2" runat="server"></asp:Literal>
&nbsp;<asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                
           <center> <table width="55%" border="1"><tr class="tr1"><td>编号</td><td>名称</td>
                  <td>价格</td><td>产地</td><td>货架</td><td>库存</td><td>购买</td></tr>
            </HeaderTemplate>

            <ItemTemplate>
                <tr class="tr2"><td><%#Eval("Ids") %></td><td><%#Eval("Name") %></td><td><%#Eval("Price") %></td><td><%#Eval("Source") %></td>
                    <td><%#Eval("Stack") %></td><td><%#Eval("Numbers") %></td><td><a href="buy.aspx?id=<%#Eval("Ids") %>">购买</a></td></tr>
            </ItemTemplate>

            <AlternatingItemTemplate>
                 <tr class="tr3"><td><%#Eval("Ids") %></td><td><%#Eval("Name") %></td><td><%#Eval("Price") %></td><td><%#Eval("Source") %></td>
                    <td><%#Eval("Stack") %></td><td><%#Eval("Numbers") %></td><td><a href="buy.aspx?id=<%#Eval("Ids") %>">购买</a></td></tr>
            </AlternatingItemTemplate>

            <FooterTemplate>
                </table></center>
            </FooterTemplate>
        </asp:Repeater>
    </div>
            </div>
    </form>
</body>
</html>

主界面aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (Session["data"]==null)
        {
            Response.Redirect("denglu.aspx");
            
        }
        DataClassesDataContext _Context = new DataClassesDataContext();
        Repeater1.DataSource = _Context.Fruit.ToList();
        Repeater1.DataBind();

        int count;
        decimal cost;
        if (Session["cart"] == null)
        {
            count = 0;
            cost = 0;
        }
        else
        {
            List<OrderDetails> list = Session["cart"] as List<OrderDetails>;
            count = list.Count();
            //计算总钱数,记住这个代码!!!!!!
            cost = list.Sum(p=>p.Count*p.FruitPrice).Value;
        }
        Literal1.Text = "您已经购买了" + count + "种水果,您当前的花费为" + cost + "";

        Session["cost"] = cost;

        Session["count"] = count;
    }
}

登录界面aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="denglu.aspx.cs" Inherits="denglu1" %>

<!DOCTYPE html>
<script runat="server">

</script>



<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
      
        #id1 {
            
            top:0px;
            left:300px;
        }
          #id2 {
             position:absolute;
            top:90px;
            left:200px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
      <div id="id2">
          <p><a href="Default.aspx">浏览商品</a></p>
     <p> <a href="zhanghu.aspx">查看账户</a></p>
         <p> <a href="gouwuche.aspx">查看购物车</a></p>
       
      </div>
    <div id="id1">
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
        <center><h1><font color="blue">大苹果购物网</font></h1></center>
        <br />
                        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </p>
                        <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 密码:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
        </p>
                                                      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                      <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
                   
    </div>
    </form>
</body>
</html>

 

登录界面aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class denglu1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["data"] == null)
        {   
            Literal1.Text = "请先登录!";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = TextBox1.Text;
        string pass = TextBox2.Text;
        Session["name"] = name;


        Login data = new LoginBF().Select(name,pass);
        string yonghuming = data.UserName;
        Session["yonghuming"] = yonghuming;


        Session["data"] = data;
       if (data != null)
       {
           Response.Redirect("Default.aspx");      
       }
       else
       {

           Response.Write("<script>alert('登录失败')</script>");         
           TextBox1.Text = null;
           TextBox2.Text = null;
       }
    }
  
}

购物车aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gouwuche.aspx.cs" Inherits="gouwuche" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
       .tr1 {
            text-align:center;
            background-color:#0094ff;
            font-weight:bolder;
        }
        .tr2 {
            text-align:center;
            background-color:#ffd800;
        }
          .tr3 {
            text-align:center;
          
        }
        #id1 {
            
            top:0px;
            left:300px;
        }
          #id2 {
             position:absolute;
            top:105px;
            left:160px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
      <div id="id2">
          <p><a href="Default.aspx">浏览商品</a></p>
     <p> <a href="zhanghu.aspx">查看账户</a></p>
         <p> <a href="gouwuche.aspx">查看购物车</a></p>
       
      </div>
    <div id="id1">
        <center><h1><font color="blue">大苹果购物网<asp:ScriptManager ID="ScriptManager2" runat="server">
            </asp:ScriptManager>
            </h1>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            </asp:UpdatePanel>
            <h1></font></h1></center>
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              购物车中有以下几种产品:
          <asp:Literal ID="Literal1" runat="server">

          </asp:Literal>
            <span  id="span1" style="display:none; color:#F66">账户余额不足</span>
            <span id="span2" style="display:none; color:#F66">苹果库存不足</span>
    
     
                    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
                   <font color="red">
             &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Literal ID="Literal3" runat="server"></asp:Literal> </br>               
            &nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:Literal ID="Literal2" runat="server"></asp:Literal> </br>
                      
                        </font>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <asp:Button ID="Button1" runat="server" Text="提交订单" OnClick="Button1_Click" />
             
               
               
            </ContentTemplate>
        </asp:UpdatePanel>
      
    </div>
    </form>
</body>
</html>

 

购物车aspx.cs代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class gouwuche : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["data"] == null)
        {
            Response.Redirect("denglu.aspx");
        }

      
            if (Session["cart"] != null)
            {
                List<OrderDetails> list = Session["cart"] as List<OrderDetails>;
                Session["linshi"] = list;
                string s = " <center><table width=' 35%'  border='1'><tr class='tr1'><td>编号</td><td>名称</td><td>价格</td><td>数量</td><td>删除</td></tr>";
                foreach (OrderDetails data in list)
                {
                    s += "<tr class='tr2'><td>" + data.FruitCode + "</td><td>" + data.FruitName + "</td><td>" + data.FruitPrice + "</td><td>" + data.Count + "</td><td><a href='shanchu.aspx?ids=" + data.FruitCode + "'>删除</a></td></tr>";
                }
                s += "</table></center>";
                Literal1.Text = s;
            }
          
        
    }
    //当点击提交订单的时候

    protected void Button1_Click(object sender, EventArgs e)
    {

        Orders data3 = new Orders();
        data3.Code = Session["name"].ToString();
        data3.UserName = Session["yonghuming"].ToString();
        data3.OrderTime = DateTime.Now;
        new OrderBF().Insert(data3);



        string name = Session["name"].ToString();
        Login data = new LoginBF().Select(name);
        decimal zhanghu = Convert.ToDecimal(data.Account);
        decimal cost =Convert.ToDecimal( Session["cost"]);
        decimal yue = zhanghu-cost;
        Session["yue"] = yue;

        if (zhanghu >=cost)
        {

            Literal2.Text = "您的账户余额为" + yue + "";
        }
        else if(cost > zhanghu)
        {
            //Response.Write("<script> span1.style.display='block'</script>");
            Literal2.Text = "账户余额不足!";
            Button1.Enabled = false;
        }


        ArrayList all = new ArrayList();
        //List<OrderDetails> list= Session["cart"] as List<OrderDetails>;
        List<OrderDetails> list = Session["linshi"] as List<OrderDetails>;
        //a是购买le几种水果
        int a = Convert.ToInt32(Session["count"]);
        //把购买的水果编号放在集合里
        foreach (OrderDetails data2 in list)
        {
            all.Add(data2.FruitCode);
         
        }


        //通过购买的水果编号来查询这个水果编号的库存并进行比较
        //这个集合用来放购买的库存不足的水果名称
        ArrayList allshuiguo = new ArrayList();
        Fruit data1 = new Fruit();
        string neirong = "";
        for (int i = 0; i < a; i++)
        {
            //通过购物车里的水果编号查询水果表相对应的水果库存
             data1 = new FruitBF().Select(all[i].ToString());
             if (list[i].Count<data1.Numbers)
             {
                 allshuiguo.Add(list[i].FruitName);
                 neirong = neirong + allshuiguo[i].ToString()+"";
                 Literal3.Text = neirong +""+allshuiguo.Count+"种水果的库存不足";
             }

             data1.Numbers = data1.Numbers - list[i].Count;
            //循环调用修改方法,对数据库的数量进行修改
             new FruitBF().update(data1);
           }

        Session.Remove("cart");
       
        }
       
    }
        
        

    

删除界面aspx 代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="shanchu.aspx.cs" Inherits="shanchu" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

删除界面aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class shanchu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ids = Request["ids"];
        if (Session["cart"] == null)
        {
            List<OrderDetails> temp = new List<OrderDetails>();
            Session["cart"] = temp;
        }
        List<OrderDetails> list = Session["cart"] as List<OrderDetails>;
        //判断删除的水果是不是只有一个
        var query = list.Where(P => P.FruitCode == ids);
        if (query.Count() > 0)
        {
            OrderDetails data = query.First();
            if (data.Count > 1) //证明以前买过,数量减减就行了
            {
                data.Count--;
               //减去一个水果后的价格和数量 
            }
            else
            {
                list.Remove(data);   
                //删除这种水果后计算的价格及删除后的种类的数量         
            }
        }


        int count;
        decimal cost;
        if (Session["cart"] == null)
        {
            count = 0;
            cost = 0;
        }
        else
        {
            List<OrderDetails> list1= Session["cart"] as List<OrderDetails>;
            count = list1.Count();
            //计算总钱数,记住这个代码!!!!!!
            cost = list.Sum(p => p.Count * p.FruitPrice).Value;
        }
        Session["cost"] = cost;
        Session["count"] = count;
        Response.Redirect("gouwuche.aspx");
    }

}      
 
            

查看账户aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="zhanghu.aspx.cs" Inherits="zhanghu" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
        #div1 {
            position:absolute;
            width:200px;
            height:150px;  
            top:100px;
            left:400px;        
            background-color:#b6ff00;
        }
    
    </style>
</head>
<body>
   
        <center><h1><font color="blue">大苹果购物网</font></h1></center>
      

            <br/>
            <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="Default.aspx">浏览商品</a></p>
             <p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="zhanghu.aspx">查看账户</a></p>
             <p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="gouwuche.aspx">查看购物车</a></p>
             <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="denglu.aspx">登录</a></p>

          <div id="div1">
            <asp:Literal ID="Literal1" runat="server"></asp:Literal>
         </div>
          

</body>
</html>

 

查看账户aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class zhanghu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
     
        if (Session["data"] == null)
        {
            Response.Redirect("denglu.aspx");
        }
        else
        {
            string user = Session["name"].ToString();
            Login data = new LoginBF().Select(user);
            if (Session["cart"] == null)
            {
         
                Literal1.Text = "您的账户余额为" + data.Account + "";
            }
            else
            {
                decimal cost = Convert.ToDecimal( Session["cost"]);
             
                Literal1.Text = "您的账户余额为" + (data.Account-cost) + "";
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/275147378abc/p/4673161.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值