Webform购物车(用Session存储,页面传值)

购物车主要实现的功能:

①在主页面可以将所有商品显示出来,包括价格,库存。

②点击购买可以累加产品,如果是同一种产品,只会累加每种产品的数量。

③查看购物车,可以查看明细,包括所购物品的名称,价格,数量等等。

  ④查看账户,显示账户余额。

⑤在购物车中可以下订单,当点击下订单的时候,需要判断是否已经登录,如果没有登录,则跳到登录页面。

⑥下订单之后,客户的余额减少幅度等于所消费幅度。

  ⑦余额不足,提示;库存不足,提示;购物车清空。

⑧库存产品数量减少,减少的每种产品的数量等于购买的每种产品的数量。

下面用代码来详细介绍一下购物车功能的实现:

Webform购物车(用Session存储,页面传值)

先创建一个母版页:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div style="background-color:#6aa3e1;">
<div style="font-size:30px; color:#fff; font-family:微软雅黑;margin-top:20px;">
   
<asp:Label ID="Label1" runat="server" Text="大苹果购物网" ></asp:Label>
</div> <div style=" width:100%;height:25px;font-size:20px; color:#ff00dc;">
<a href="Login.aspx" style="float:right;margin-right:15px;">登录</a>
</div> <div style="width:100%;height:360px">
<div style="width:20%;height:360px; float:left;">
<div style="margin-top:110px; text-align:center; font-size:20px;">
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click1">浏览商品</asp:LinkButton>
</div>
<div style="margin-top:20px; text-align:center; font-size:20px;">
<asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">查看账户</asp:LinkButton>
</div>
<div style="margin-top:20px; text-align:center; font-size:20px;">
<asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">查看购物车</asp:LinkButton>
</div>
</div>
<div style="width:80%;height:360px; float:left;background-color:#fff;">
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder>
</div>
</div>
<div style="width:100%;height:40px;text-align:center;line-height:40px;">
汉企教育科技出品
</div>
</div>
</form>
</body>
</html>

实现用户登录功能:

Webform购物车(用Session存储,页面传值)

Login.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
            <div style="width:50%;text-align:center">
                        <div style="height:45px;background:#4256f2;">
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="Label2" runat="server" Font-Names="微软雅黑" Font-Size="XX-Large" ForeColor="White" Text="登    录" ></asp:Label>
                        </div>
                        <div style="height:315px;background:#7e8ae5">
                            <br />
                            <br />
                            <br />
                            <asp:Label ID="Label3" runat="server" Text="用户名:" Font-Size="Larger"></asp:Label>
                            <asp:TextBox ID="txtuid" runat="server" Font-Size="Large" Width="185px"></asp:TextBox>
                            <br />
                            <br />
                            <br />
                            <asp:Label ID="Label4" runat="server" Text="密码:" Font-Size="Larger"></asp:Label>
&nbsp;
                            <asp:TextBox ID="txtpwd" runat="server" Font-Size="Large" Width="185px" TextMode="Password"></asp:TextBox>
                            <br />
                            <br />
                            <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="Button1" runat="server" Font-Size="Larger" Text="登录" OnClick="Button1_Click" />
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <br />
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <br />
                        </div>
        </div>
</asp:Content>

Login.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)
{
bool isok = new FruitDA().checkUser(txtuid.Text, txtpwd.Text);
if (isok)
{
Session["uid"] = txtuid.Text;
Response.Redirect("Index.aspx?user=" + txtuid.Text);//跳转页面 }
}
protected void Button1_Click(object sender, EventArgs e)
{ }
}

Index页面,实现功能①,显示所有物品。

Webform购物车(用Session存储,页面传值)

Index.aspx代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
        <HeaderTemplate>
            <table style="width:100%; height:70%; text-align:center; background:#000000" >
                 <tr style=" color:#fff;">
                                <td>代号</td>
                                <td>水果名称</td>
                                <td>水果价格</td>
                                <td>原产地</td>
                                <td>货架</td>
                                <td>库存量</td>
                                <td></td>
                            </tr>           
        </HeaderTemplate>         <ItemTemplate>
            <tr style="background:#fff">
                                <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><asp:LinkButton ID="LinkButton1" CommandName="buy" CommandArgument='<%#Eval("ids") %>' runat="server">购买</asp:LinkButton></td>
                            </tr>
        </ItemTemplate>         <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
</asp:Content>

实现功能②,点击购买时,存放在session[]里面

Index.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 Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Repeater1.DataSource = new FruitDA().select();
            Repeater1.DataBind();
        }
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "buy")  //通过commandname区分点击的是购买按钮
        {
            string ids = e.CommandArgument.ToString();
            if (Session["list"] != null)
            {
                List<Fruit> slist = (List<Fruit>)Session["list"];
                int count = 0;  //标记变量,判断购物车中有没有选中的商品
                foreach(Fruit data in slist)
                {
                    if (ids == data.Ids)    //购物车中存在选中的商品
                    {
                        data.Numbers += 1;//存在的商品数量+1
                        count++;
                    }
                }
                if (count == 0)//购物车中不存在选中的商品
                {
                    Fruit data = new FruitDA().selectbyids(ids);
                    data.Numbers = 1;   //购物车中选中的商品数量为1
                    slist.Add(data);
                    Session["list"] = slist;
                }
            }
            else  //购物车中一件商品也没有
            {
                List<Fruit> list = new List<Fruit>();
                Fruit data = new FruitDA().selectbyids(ids);
                data.Numbers = 1;
                list.Add(data);
                Session["list"] = list;
            }
            //计算购物车商品数量及总价
            if(Session["list"] != null)
            {
                List<Fruit> list = (List<Fruit>)Session["list"];
                decimal sum = 0;
                foreach(Fruit data in list)
                {
                    sum += (decimal)(data.Numbers * data.Price);
                }
                 Literal1.Text = "购物车中有 "+list.Count+" 件商品,总价为 "+sum+" 元";
            }         }
    }
}

实现功能③,查看购物车

Webform购物车(用Session存储,页面传值)

ViewCart.aspx代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ViewCart.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div>
        <asp:Literal ID="Literal2" runat="server"></asp:Literal>
    </div>
    <div style="width:640px;height:200px;">
    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
        <HeaderTemplate>
            <table style="width:600px; text-align:center; background:#000000">
                 <tr style=" color:#fff;">
                                <td>代号</td>
                                <td>商品名称</td>
                                <td>商品价格</td>
                                <td>购买数量</td>
                                <td></td>
                            </tr>           
        </HeaderTemplate>

<ItemTemplate>
            <tr style="background:#fff">
                                <td><%#Eval("ids") %></td>
                                <td><%#Eval("name") %></td>
                                <td><%#Eval("price") %></td>
                                <td><%#Eval("numbers") %></td>
                                <td><asp:LinkButton ID="LinkButton1" CommandName="delete" CommandArgument='<%#Eval("ids") %>' runat="server">删除</asp:LinkButton></td>
                            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
        </div>
        <div style="width:600px;height:50px;">
            <asp:Literal ID="Literal1" runat="server"></asp:Literal><br />
            &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:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">提交订单</asp:LinkButton>
            <br />
        </div>
</asp:Content>

实现功能⑤⑥⑦⑧,点击购买时,存放在session[]里面

ViewCart.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 (!IsPostBack)
        {
            if (Session["list"] != null)
            {
                Repeater1.DataSource = (List<Fruit>)Session["list"];//里氏转换,购物车的表存在session中
                Repeater1.DataBind();
            }
            else
            {
                Literal2.Text = "购物车中没有任何商品!";
            }
        }
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "delete")  //删除按钮
        {
            string ids = e.CommandArgument.ToString();
            List<Fruit> list = (List<Fruit>)Session["list"];
            Fruit sdata = null; //做标记
            foreach (Fruit data in list)
            {
                if (ids == data.Ids)//找到商品
                {
                    if (data.Numbers > 1)//如果商品数量大于1
                    {
                        data.Numbers -= 1;
                    }
                    else//否则数量等于1
                    {
                        sdata = data;
                    }
                }
            }
            if (sdata != null)
            {
                list.Remove(sdata);//直接移除商品
            }
            Session["list"] = list;
            Repeater1.DataSource = (List<Fruit>)Session["list"];//重新绑定数据
            Repeater1.DataBind();
        }
    }     //先验证库存是否足够,用户是否登陆,当二者都满足时,然后在进数据库执行删除
    protected void LinkButton2_Click(object sender, EventArgs e)
    {         List<Fruit> list = (List<Fruit>)Session["list"];
        bool isok = true;
        foreach (Fruit data in list)
        {
            var s1 = data.Numbers;
            var s2 = new FruitDA().selectbyids(data.Ids).Numbers;
            if (s1 < s2)//判断购买的数量和库存数量
            {
                isok = true;
            }
            else
            {
                isok = false;
                string name = new FruitDA().selectbyids(data.Ids).Name;
                Literal1.Text = name + "的库存不足!";
            }
        }
        //如果库存满足,再进行验证用户是否登录
        if (isok)
        {
            //如果用户登录成功,再判断该用户的余额是否满足购买商品的总额
            if (Session["uid"] != null)
            {
                string uid = Session["uid"].ToString();
                var yu = new FruitDA().selectuser(uid).Account;//用户的余额
                decimal sum = 0;
                foreach (Fruit data in list)
                {
                    sum += (decimal)(data.Numbers * data.Price);//商品的总价
                }
                if (yu >= sum)//余额满足
                {
                    foreach (Fruit sdata in list)//减库存
                    {
                        var s1 = sdata.Numbers;//购买的数量
                        var s2 = new FruitDA().selectbyids(sdata.Ids).Numbers;//库存
                        var s = s2 - s1;
                        Fruit data = new Fruit();
                        data.Ids = sdata.Ids;
                        data.Numbers = s;
                        new FruitDA().update(data);
                    }
                    //减余额
                    var shengyu = yu - sum;
                    Login m = new Login();
                    m.UserName = uid;
                    m.Account = shengyu;
                    new FruitDA().updateyue(m);
                    //添加订单
                    Orders data1 = new Orders();
                    Random r = new Random();
                    string ordercode = DateTime.Now.ToString("yyyyMMddHHmmssms") + r.Next(100).ToString();
                    data1.Code = ordercode;
                    data1.UserName = uid;
                    data1.OrderTime = DateTime.Now;                     new FruitDA().Insert1(data1);                     //添加订单详情表
                    foreach (Fruit data in list)
                    {
                        OrderDetails data2 = new OrderDetails();
                        data2.OrderCode = ordercode;
                        data2.FruitCode = data.Ids;
                        data2.Count = data.Numbers;                         new FruitDA().Insert2(data2);
                    }
                    Literal1.Text = "订单提交成功!";
                }
                else//如果余额不满足,进行提示
                {
                    Literal1.Text = "余额不足!";
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }     }
}

实现功能④,查看账户余额

ViewAccount.aspx代码

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ViewAccount.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</asp:Content>

ViewAccount.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["uid"] != null)
{
string uid = Session["uid"].ToString();
var yu = new FruitDA().selectuser(uid).Account;//用户的余额
Literal1.Text = "您的账户中还剩余 "+yu+" 元";
}
else
{
Response.Redirect("Login.aspx");
}
}
}
上一篇:Flex + .Net从本地选择一个图片上传到服务器


下一篇:js中继承的几种用法总结(apply,call,prototype)