码迷,mamicode.com
首页 > Web开发 > 详细

webform之session传值(临时数据的存储)与扩展属性 --(购物车练习)

时间:2015-10-14 23:49:19      阅读:476      评论:0      收藏:0      [点我收藏+]

标签:

页面传值:
1.QueryString传值
在源页面写:Response.Redirect("Main.aspx?uid="+uid+"&pwd="+pwd);
在目标页面:Request["uid"].ToString();
2.Session *****
特点:可以存任何东西,每个用户都会生成一个特定的Session,Session是存储在服务中的,
一般默认存储20分钟,20分钟之后过期
用法:在登录页面:Session["uid"] = uid;
在使用页面:Session["uid"].ToString();
3.Cookie *****
特点:默认永不过期,可以设置它的过期时间,Cookie是存储在客户端的
用法:在登录界面: //新建Cookie对象
HttpCookie cookie_name = new HttpCookie("uid");
//赋值
cookie_name.Value = uid;
//写入到Cookie里面
Response.AppendCookie(cookie_name);

在使用界面:Request.Cookies["uid"].Value.ToString(); 
4.Application
特点:它是共有的
用法:在源页面: Application["uid"] = uid;
在目标页面: Application.Lock();
Label1.Text = Application["uid"].ToString();
Application.UnLock();

例题  购物车:

目标 :如何用session与list集合的转换 从何搭建一个临时的数据库表

1、浏览商品界面

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

<!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>
        <h1>大苹果购物网</h1>
        <p>&nbsp;</p>
        
        <div style="float:left; width:150px; height:500px;">

            <br />
            <br />
            <br />
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">浏览商品</asp:LinkButton>
            <br />
            <br />
            <br />
            <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">查看账户</asp:LinkButton>
            <br />
            <br />
            <br />
            <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">查看购物车</asp:LinkButton>

        </div>
        <div style =" float:left; height:500px;">

           
          
          


            <asp:Repeater ID="Repeater1" runat="server">
                   <HeaderTemplate>
                        <table width="600" border="0" cellspacing="1" cellpadding="0" bgcolor="#6600FF">
                          <tr>
                            <td height="25" bgcolor="#FFFFFF">代号</td>
                            <td bgcolor="#FFFFFF">水果名称</td>
                            <td bgcolor="#FFFFFF">水果价格</td>
                            <td bgcolor="#FFFFFF">源产地</td>
                            <td bgcolor="#FFFFFF">库存</td>
                            <td bgcolor="#FFFFFF">操作</td>
                          </tr>
                   </HeaderTemplate>
                    <ItemTemplate>

                            <tr>
                                <td height="25" bgcolor="#FFFFFF"><%#Eval("Ids") %></td>
                                <td bgcolor="#FFFFFF"><%#Eval("Name") %></td>
                                <td bgcolor="#FFFFFF"><%#Eval("Price") %></td>
                                <td bgcolor="#FFFFFF"><%#Eval("Source") %></td>
                                <td bgcolor="#FFFFFF"><%#Eval("Numbers") %></td>
                                <td bgcolor="#FFFFFF"><a href="ChuLi.aspx?code=<%#Eval("Ids") %>">购买</a></td>
                              </tr>
                    </ItemTemplate>
                <FooterTemplate>
                      </table>
                </FooterTemplate>
            </asp:Repeater>

            <br />

            <br />
            <asp:Label ID="lblTs" runat="server"></asp:Label>

        </div>

 

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 Main : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["uid"] != null)
            {
                FirutDataContext context = new FirutDataContext();

                Repeater1.DataSource = context.Fruit;
                Repeater1.DataBind();

                if (Session["Cart"] != null)
                {
                    List<OrderDetails> list = Session["Cart"] as List<OrderDetails>;

                    int sl = list.Count();
                    decimal zj = Convert.ToDecimal( list.Sum(p => p.Count * p.JiaGe));

                    lblTs.Text = "购物车中有:"+sl+"件商品,总价为:"+zj+"元。";

                }

            }
            else
            {
                Response.Redirect("DengLu.aspx");
            }
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Main.aspx");
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect("YuE.aspx");
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Response.Redirect("GouWuChe.aspx");
    }
}

 

2、点击购买界面:

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 ChuLi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

         string code = Request["code"].ToString();

        if (Session["Cart"] != null)
        {

            List<OrderDetails> listall = Session["Cart"] as List<OrderDetails>;

            var query = listall.Where(p => p.FruitCode == code);
            if (query.Count() > 0)
            {
                OrderDetails data1 = query.First();
                //listall.Remove(data1);
                data1.Count = data1.Count + 1;
                //listall.Add(data1);

                Session["Cart"] = listall;

            }
            else
            {
                OrderDetails data2 = new OrderDetails();

                data2.FruitCode = code;
                data2.Count = 1;

                listall.Add(data2);

                Session["Cart"] = listall;
            }
            


        }
        else
        {
            FirutDataContext context = new FirutDataContext();

            List<OrderDetails> list = new List<OrderDetails>();

            OrderDetails data = new OrderDetails();

            data.FruitCode = code;
            data.Count = 1;

            list.Add(data);

            Session["Cart"] = list;


        }

        Response.Redirect("Main.aspx");
        
    }
}

 

 

 

 

3、浏览购物车界面

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

<!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>
        <h1>购物车</h1>
        <p>&nbsp;</p>
        
        <div style="float:left; width:150px; height:500px;">

            <br />
            <br />
            <br />
            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">浏览商品</asp:LinkButton>
            <br />
            <br />
            <br />
            <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click">查看账户</asp:LinkButton>
            <br />
            <br />
            <br />
            <asp:LinkButton ID="LinkButton3" runat="server" OnClick="LinkButton3_Click">查看购物车</asp:LinkButton>

        </div>
        <div style =" float:left; height:500px;">

           
          
          


            <asp:Repeater ID="Repeater1" runat="server">
                   <HeaderTemplate>
                        <table width="600" border="0" cellspacing="1" cellpadding="0" bgcolor="#6600FF">
                          <tr>
                            <td height="25" bgcolor="#FFFFFF">商品名称</td>
                            <td bgcolor="#FFFFFF">商品单价</td>
                            <td bgcolor="#FFFFFF">购买数量</td>
                            <td bgcolor="#FFFFFF">操作</td>
                          </tr>
                   </HeaderTemplate>
                    <ItemTemplate>

                            <tr>
                                <td height="25" bgcolor="#FFFFFF"><%#Eval("ShangPinName") %></td>
                                <td bgcolor="#FFFFFF"><%#Eval("JiaGe") %></td>
                                <td bgcolor="#FFFFFF"><%#Eval("Count") %></td>
                                <td bgcolor="#FFFFFF"><a href="ShanChu.aspx?code=<%#Eval("FruitCode") %>">删除</a></td>
                              </tr>
                    </ItemTemplate>
                <FooterTemplate>
                      </table>
                </FooterTemplate>
            </asp:Repeater>

            <br />

            <br />
            <asp:Label ID="lblTs" runat="server" ForeColor="#CC0000"></asp:Label>

            <br />
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="提交订单" />
            <br />
            <br />
            <asp:Label ID="lblSure" runat="server" ForeColor="#CC0000"></asp:Label>
            <br />

        </div>

    
    </div>
    </form>
</body>
</html>

 

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 GouWuChe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Session["Cart"] != null)
            {
                List<OrderDetails> list = Session["Cart"] as List<OrderDetails>;
                Repeater1.DataSource = list;
                Repeater1.DataBind();


            }
            else
            {
                lblTs.Text = "购物车中没有任何商品!";
            }


        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Main.aspx");
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect("YuE.aspx");
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Response.Redirect("GouWuChe.aspx");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<OrderDetails> list = Session["Cart"] as List<OrderDetails>;

        FirutDataContext context = new FirutDataContext();

        bool IsOk = true;

        foreach (OrderDetails data in list)
        {
            int sl = data.Count.Value;

            var query = context.Fruit.Where(p=>p.Ids == data.FruitCode);

            int kc = query.First().Numbers.Value;

            if (sl <= kc)
            {
                IsOk = IsOk && true;
            }
            else
            {
                IsOk = IsOk && false;
                lblSure.Text += query.First().Name +"的库存不足! ";
            }


        }

        if (IsOk)
        {
            if (Session["uid"] != null)
            {
                string uid = Session["uid"].ToString();

                var query = context.Login.Where(p=>p.UserName == uid);

                decimal ye = query.First().Account.Value;

                decimal zj = list.Sum(p => p.JiaGe * p.Count).Value;

                if (ye >= zj)
                {
                    //减库存
                    foreach (OrderDetails data in list)
                    {
                        int sl = data.Count.Value;

                        var query2 = context.Fruit.Where(p => p.Ids == data.FruitCode);

                        query2.First().Numbers = query2.First().Numbers.Value - data.Count;

                        context.SubmitChanges();

                    }

                    //减余额
                    var query3 = context.Login.Where(p=>p.UserName == uid);
                    query3.First().Account = query3.First().Account.Value - zj;
                    context.SubmitChanges();

                    //添加订单
                    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;

                    context.Orders.InsertOnSubmit(data1);
                    context.SubmitChanges();

                    //添加订单详情表

                    foreach (OrderDetails data in list)
                    {
                        data.OrderCode = ordercode;

                    }
                    context.OrderDetails.InsertAllOnSubmit(list);
                    context.SubmitChanges();

                }
                else
                {
                    lblSure.Text +="余额不足!";
                }
            }
            else
            {
                Response.Redirect("DengLu.aspx");
            }


        }




    }
}

 

4、购物车 中 点击 删除 ,操作界面

 

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)
    {
        List<OrderDetails> list = Session["Cart"] as List<OrderDetails>;

        string code = Request["code"].ToString();


        var query = list.Where(p => p.FruitCode == code);

        if (query.First().Count.Value > 1)
        {
            query.First().Count--;
        }
        else
        {
            list.Remove(query.First());
        }

        Session["Cart"] = list;

        Response.Redirect("GouWuChe.aspx");
        
    }
}

 

 

5、查看 账户 界面(无)

 6、扩展 orderdetails属性(李老师版本)

public partial class OrderDetails
{
    private decimal jiaGe;

    public decimal JiaGe
    {
        get 
        {
            FirutDataContext context = new FirutDataContext();

            var query = context.Fruit.Where(p=>p.Ids == FruitCode);

            Fruit data = query.First();


            return  data.Price.Value; 
        }
        set { jiaGe = value; }
    }

    private string shangPinName;

    public string ShangPinName
    {
        get {
            FirutDataContext context = new FirutDataContext();

            var query = context.Fruit.Where(p => p.Ids == FruitCode);

            Fruit data = query.First();


            return data.Name; 
        }
        set { shangPinName = value; }
    }

 

另一版本

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();
        }
     
    }

 如两个表 有直接相关的外键关系(例如nation 表与 info 表中的nation 属性 )

public partial class Info
{
    public string Nname
    {
        get
        {
            return this.Nation1.Name;
        }
    }
}

 

webform之session传值(临时数据的存储)与扩展属性 --(购物车练习)

标签:

原文地址:http://www.cnblogs.com/zyh-club/p/4880957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!