2012-10-20 31 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