2014-04-18 2 views
0

제품 및 결제 페이지를 만들고 있습니다. 버튼 내가 카트 페이지에서 다음이어떻게 재정의 한 후에도 이전 세션을 유지할 수 있습니까?

Session["code"] = productLabel.Text; 
Session["description"] = descriptionTextBox.Text; 
Session["price"] = priceLabel.Text; 
Response.Redirect("cart.aspx"); 

을 누르면 제품 페이지에서 내가 다음 목록에 다른 제품을 추가 할 때 그러나 내 문제는,이 작동

if ((Session["code"] != null)) 
    {code = (Session["code"]).ToString();} 

if ((Session["description"] != null)) 
    { description = (Session["description"]).ToString(); } 

if ((Session["price"] != null)) 
    {price = (Session["price"]).ToString(); } 

    string item = code + " " + description + " " + price; 
    cartList.Items.Add(item); 

이, 내 첫 번째 항목을 재정의하므로 한 번에 한 항목 만 존재합니다. 현재/이전에 있었던 것을 어떻게 추적 할 수 있습니까?

고맙습니다.

답변

1

전체 개념을 재고하고 맞춤 클래스를 대신 저장하는 것이 좋습니다. 한 가지 할 수있는 것은 장바구니에 항목 목록을 만들어 세션에 저장하는 것입니다.

[Serializable] 
public class Item 
{ 
    public string Code {get;set;} 
    public string Description {get;set;} 
    public string Price {get;set;} 
} 

List<Item> cart=new List<Item>(); 
Item item=new Item(); 
item.Code=productLabel.Text; 
item.Description=descriptionTextBox.Text; 
item.Price=priceLabel.Text; 
cart.Add(item); 
Session["cart"]=cart; 

//then later pull it out... 
List<Item> cart=Session["cart"] as List<Item>; //youll want to check for null etc 
//and add another item 
Item newItem=new Item(); 
newItem.Code=productLabel.Text; 
newItem.Description=descriptionTextBox.Text; 
newItem.Price=priceLabel.Text; 
cart.add(newItem); 

아키텍처에 문제가 있습니다. 예를 들어, 진취적인 개인은 브라우저 도구를 사용하여 priceLabel.Text의 값을 변경하고 잠재적으로 비용을 덜 지불 할 수 있습니다. 그러나 잘하면이 진행 방법에 대한 아이디어를 제공합니다.

public static readonly ConcurrentDictionary<string,List<Item>> myMemoryCache = new ConcurrentDictionary<string,List<Item>>(); 

를 데이터 소스로 사용 :

-1
public class Item 
{ 
    public string Code {get;set;} 
    public string Description {get;set;} 
    public string Price {get;set;} 
} 

당신은 당신의 항목을 저장하는 메모리 캐시에 종류의 같은 만들 수 있습니다.

여러 항목에 대해 동일한 '키'를 사용하거나 원하는 항목으로 변경할 수 있습니다.

App_Start에서 초기화하십시오.

관련 문제