页面传值:
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;
}
}
}

如果session在一般处理程序集中为null,需要引用using System.Web.SessionState;

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

  1. 分布式监控系统开发【day37】:监控数据如何存储(七)

    一.如何存储 二.目录结构 三.代码调用逻辑关系 四.实现代码 1.data_optimization 1.存筛选出来符合条件的数据 def get_data_slice(self,lastest_d ...

  2. 临时存存储页面上的数据---Web存储

    HTML5 Web存储的两种方法使用 localStorage和sessionStorage 参考: http://www.cnblogs.com/taoweiji/archive/2012/12/0 ...

  3. ArcEngine临时数据存储 创建内存工作空间

    参考网址,这里 工作中有时候需要使用临时数据,以前都是创建一个默认的shapefile或者gdb,今天发现esri官方帮助文档给出了一个方法,可以创建内存工作空间,代码如下: public stati ...

  4. webform中Session和Cookie对象的用法

    Session: Session:在计算机中,尤其是在网络应用中,称为"会话控制".Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web页之间 ...

  5. WebForm多页面传值跳转

    一.URL传值 URL传值是利用跳转地址直接加变量定义内容 格式:跳转地址?任意变量=传的值--?=之间不能有空格 多条数据传值 在地址栏继续拼接&key=value void Button1 ...

  6. 【分布式】Zookeeper数据与存储

    一.前言 前面分析了Zookeeper对请求的处理,本篇博文接着分析Zookeeper中如何对底层数据进行存储,数据存储被分为内存数据存储于磁盘数据存储. 二.数据与存储 2.1 内存数据 Zooke ...

  7. Chrome扩展开发之三——Chrome扩展中的数据本地存储和下载

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  8. magento里的session传值

    1.$registrationCode = Mage::getSingleton('customer/session' )->setData('login_phone_code', $valid ...

  9. (52)Wangdao.com第七天_字面量/变量_标识符_数据类型_数据的存储

    JavaScript 字面量 和 变量 字面量:就是那些不可变的值,如1,2,100,2000,Infinity,NaN 变量: 变量,代表的当前随机分配的内存地址. 变量的值,是可变的,可以用来保存 ...

随机推荐

  1. codeigniter IE浏览器下无法登录的解决的方法

    站点搬迁到新的server后,CI 框架做的站点IE浏览器下无法登录.登录时候採用CI自带的SESSION机制,事实上是以COOKIE保存. 网上搜索到IE浏览器不支持域名存在- _. 不是这个原因, ...

  2. SQL中的Update、delete与inner join 联合使用

    Update XXX set XXX where 这种写法大家肯定都知道,才发现update和delete居然支持inner join的update方式,太神奇了. update的格式是 update ...

  3. Maven导入时,Cannot change version of project facet Dynamic Web Module to 3.0.

    今天手贱,在eclipse里面把项目删掉了,重新maven导入时,报出Cannot change version of project facet Dynamic Web Module to 3.0. ...

  4. cocos2d-x学习资源汇总(持续更新。。。)

    引用地址:http://www.cnblogs.com/zilongshanren/archive/2012/02/17/2356516.html 我之前一直学习c++的,第一次接触cocos2d是o ...

  5. JavaSE学习总结第16天_集合框架2

      16.01 ArrayList存储字符串并遍历 ArrayList类概述:底层数据结构是数组,查询快,增删慢,线程不安全,效率高 ArrayList类是List 接口的大小可变数组的实现.实现了所 ...

  6. jQuery.form 中的 ajaxForm() 和 ajaxSubmit()

    官方例子  http://malsup.com/jquery/form/#ajaxForm官方API   http://malsup.com/jquery/form/#api中文API   http: ...

  7. sql server单表导入、导出

    sql server单表导入.导出(通过CSV文件) 导出:直接打开查询分析器查询要导出表的信息(select *  from 表),得到的结果全选,右键另存为 xxx.csv文件  (得到该表的所有 ...

  8. QListWidget 去掉选中项

    1.先去掉选中项的虚线框: 设置为无焦点:把FocusPolicy设置成:NoFocus 2.设置QListWidgetItem标志 调用setFlags函数,去掉Qt::ItemIsSelectab ...

  9. android自动化(appium)

    目录 一.Appium环境搭建 1.下载nodejs,并安装 2.下载appium,并安装 3.安装python.安装pip.安装appium 4.安装java的jdk 5.安装andriod的sdk ...

  10. C++惯用法:通过成员模板实现隐式转换(Coercion 强迫 by Member Template)

    Intent To increase the flexibility of a class template's interface by allowing the class template to ...