WebForm Session value (storage of temporary data) and extended properties--(Shopping cart practice)

Source: Internet
Author: User

Page Pass Value:
1.QueryString Transmit Value
Write on the source page: Response.Redirect ("main.aspx?uid=" +uid+ "&pwd=" +pwd);
On the target page: request["UID"]. ToString ();
2.Session * * * *
Features: Can save anything, each user will generate a specific session,session is stored in the service,
General default storage 20 minutes, 20 minutes after expiration
Usage: On the login page: session["UID"] = UID;
On use page: session["UID"]. ToString ();
3.Cookie * * * *
Features: Default never expires, you can set its expiration time, cookie is stored in the client's
Usage: At Login interface://New Cookie Object
HttpCookie cookie_name = new HttpCookie ("UID");
Assign value
Cookie_name. Value = UID;
Write to Cookie Inside
Response.appendcookie (Cookie_name);

In use interface: request.cookies["UID"]. Value.tostring ();
4.Application
Features: It is a common
Usage: On the source page: application["UID"] = UID;
On the target page: Application.Lock ();
Label1.Text = application["UID"]. ToString ();
Application.UnLock ();

Example Shopping Cart:

Goal: How to build a temporary database table with the conversion of the session and list collection

1. Browse the Product interface

<%@ Page Language= "C #"AutoEventWireup= "true"CodeFile= "Main.aspx.cs"Inherits= "Main"%><! DOCTYPE html>#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 ")%>" > Purchase </a> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp :repeater> <br/> <br/> <asp:label id= "lblts" runat= "Server" ></asp:Label> </div> 

CS Code

UsingSystem;usingSystem.collections.generic;usingSystem.linq;usingSystem.web;usingSystem.web.ui;usingSystem.web.ui.webcontrols;public Partial classmain:system.web.ui.page{protected void Page_Load (objectsender, 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 = "Shopping Cart:" +sl+ "items, Total Price:" +zj+ "Yuan. "; } } 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.re Direct ("yue.aspx");} protected void Linkbutton3_click (Object sender, EventArgs e) {Response.Redirect (" Gouwuche.aspx "); }}

2. Click on the purchase interface:

The ASPX code has no things.

CS Code

UsingSystem;usingSystem.collections.generic;usingSystem.linq;usingSystem.web;usingSystem.web.ui;usingSystem.web.ui.webcontrols;public Partial classchuli:system.web.ui.page{protected void Page_Load (objectsender, 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. Browse the Shopping Cart interface

<%@ Page Language= "C #"AutoEventWireup= "true"CodeFile= "GouWuChe.aspx.cs"Inherits= "Gouwuche"%><! DOCTYPE html>#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")%> "> Delete </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= "Submit Order"/> <br/> <br/> < Asp:label id= "lblsure" runat= "Server" forecolor= "#CC0000" ></asp:Label> <br/> </div> </div > </form></body>        

CS Code:

UsingSystem;usingSystem.collections.generic;usingSystem.linq;usingSystem.web;usingSystem.web.ui;usingSystem.web.ui.webcontrols;public Partial classgouwuche:system.web.ui.page{protected void Page_Load (objectsender, EventArgs e) {if (!IsPostBack) {if (session["Cart"]! = NULL) {list<orderdetails> List = session["Cart"] as List<orderdetails>; Repeater1.datasource =List Repeater1.databind (); } else{Lblts.text = "There are no items in the shopping cart! "; }}} protected void LinkButton1_Click (objectsender, EventArgs e) {Response.Redirect ("main.aspx")); } protected void Linkbutton2_click (objectsender, EventArgs e) {Response.Redirect ("yue.aspx")); } protected void Linkbutton3_click (objectsender, EventArgs e) {Response.Redirect ("gouwuche.aspx")); } protected void Button1_Click (objectsender, EventArgs e) {list<orderdetails> List = session["Cart"] as List<orderdetails>; Firutdatacontext context = newFirutdatacontext (); BOOL IsOk = True; foreach (OrderDetails data inlist) {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 + "Insufficient inventory! "; }} 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) {//Minus inventory foreach (OrderDetails data inlist) {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 (); }//minus balance var query3 = context. Login.where (P=>p.username = =  uid); Query3. First (). account = Query3. First (). Account.value- zj; context. SubmitChanges (); Add Order Orders Data1 = new  orders (); Random r = new  random (), String ordercode = DateTime.Now.ToString ("yyyymmddhhmmssms") + r.next (+ ). ToString (); Data1. Code =  ordercode; data1. UserName =  uid; data1. Ordertime =  DateTime.Now; context. Orders.insertonsubmit (DATA1); Context. SubmitChanges (); Add Order Details table foreach (OrderDetails data in  list) {data. Ordercode =  Ordercode;} context. Orderdetails.insertallonsubmit (list); Context. SubmitChanges (); } else  {lblsure.text + = "Insufficient balance! ";}} else  {Response.Redirect ("denglu.aspx" )}}}}         

4, the shopping cart Click Delete, the user interface

CS Code

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& Gt ; 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. View account Interface (none)

6. Extended OrderDetails attribute (teacher Li version)

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 {Firutdataco ntext context = new firutdatacontext (); var query = context. Fruit.where (p = p.ids = = Fruitcode); Fruit data = query. First (); return data. Name; } set {shangpinname = value;}}              

A different version

 using  system;using  system.collections.generic;using  system.linq;using  system.web;/// <summary>///ORDERDETAILSBF Summary description///</summary>public partial class  orderdetails{private Dataclasse Sdatacontext _context = new  dataclassesdatacontext (); public string  fruitname {//correlated subqueries, Use the fruit number in the order table to query fruit table fruit name 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); (Query. Count () > 0 ) {return  query. First (). Price.value; } return 0 ;}} public list<orderdetails>  Select () {return  _context.orderdetails.tolist ();}}     

For example, two tables have directly related foreign key relationships (such as the nation table and the Nation property in the info table)

public partial class info{public    string nname    {        get        {            return this. nation1.name;        }}}

WebForm Session value (storage of temporary data) and extended properties--(Shopping cart practice)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.