2012-10-20 26 views
0

在會話對象中存儲值是否正確?在會話中存儲值的正確方法

我試過了,但沒有正常工作。 我的意思是,存儲在會話中的值無法正確訪問或存儲。 任何人都可以幫我解決這個問題嗎? 謝謝。

protected void Page_Load(object sender, EventArgs e) 
     {  
      if (!IsPostBack) 
      {    
       if (Session["cart_table"] == null) 
       { 
        DataTable dt = new Spcart().GetCart(); 
        Session["cart_table"] = dt;     
       } 

       if (Session["ptax"] == null) 
       { 
        Session["ptax"] = 0; 
       } 
       if (Session["subtotal"] == null) 
       { 
        Session["subtotal"] = 0; 
       } 

        BindCartListView(); 
       } 
     } 

public void BindCartListView() 
    { 
     -----------------------    //some code 

     int tax=100; 
     int total=300; 
     int[] totals; 
     totals = bindtotal(tax, total); 

     -----------------------------------  //some code 
    } 

public int[] bindtotal(int tax, int total) 
    { 

     int ptax = (int)Session["ptax"]; 
     ptax += tax; 
     Session["ptax"] = ptax; 

     int subtotal = (int)Session["subtotal"]; 
     subtotal += total; 
     Session["ptax"] = subtotal; 

     int granttotal = ptax + subtotal; 
     Session["granttotal"] = granttotal; 

     int[] totals = { subtotal, granttotal }; 
     return totals; 
    }  
+0

你已經發布的代碼看起來是正確的,只要它去。什麼是不正確的工作? – McGarnagle

+0

@dbaseman:有時會話[「ptax」]和會話[「小計」]值變爲零,有時這些值不同,因爲頁面加載時想要/應該b –

+0

您正在初始化頁面加載時將會話值初始化爲0假設這些變量只在本頁面中使用) – NoviceProgrammer

回答

0

雅這似乎是正確的,但如果它是給任何錯誤,那麼請張貼error..otherwise它看起來OK