如何在所有页面中使用变量的会话 [英] How to use a session for a variable in all pages

查看:68
本文介绍了如何在所有页面中使用变量的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的购物车项目中,我使用'Home.aspx'作为起始页。我使用变量'v_MyCart'来存储选择显示的产品数量。要在所有其他页面中使用此变量,我使用称为Session [mycart]的会话。此会话的超时为20毫秒。在'AddToCart.aspx'页面中,每当我点击一个'AddToCart按钮'时,该值就会增加1,并相应地显示。但是,如果我选择主页按钮,则从AddToCart页面,主页页面的显示变为零。我知道这是因为'Session [mycart] = v_MyCart;'。如果我评论这一行并在20 mts的差距之后运行项目会发生什么,一个异常反映:对象引用未设置为对象的实例。

如何纠正此错误,即每当我在20分钟的时间范围内点击主页按钮时,v_MyCart中的值不应该变为零。





In my shopping cart project, I use 'Home.aspx' as start page. I use a variable 'v_MyCart' to store the number of products selected for display. To use this variable in all other pages I use a session called as 'Session["mycart"]. The timeout for this session is 20mts. In 'AddToCart.aspx' page the value is incremented by one whenever I click one 'AddToCart button' and is displayed accordingly. But, from 'AddToCart' page if I select 'Home' button the display at 'Home' page becomes zero. I understand that this happens because of line 'Session["mycart"]=v_MyCart;'. If I comment this line and run the project after a gap of 20 mts what happens is , an exception reflects:Object reference not set to an instance of an object.
How to rectify this error, that is, whenever I click the 'Home' button within 20mts of time frame, the value in v_MyCart should not become zero.


public partial class Home : System.Web.UI.Page
{
    static int v_MyCart;

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["prasiddhiConnectionString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["mycart"] =  v_MyCart;
            v_MyCart = (int)Session["mycart"];
            LblMyCart.Text = Convert.ToString(v_MyCart);
            LblMyCart.Visible = true;

推荐答案

访问此处...





http://msdn.microsoft.com/en-us/library/ms178581.aspx [ ^ ]







http://stackoverflow.com/questions / 8049341 / how-pass-session-variables-two-to-more-aspx-pages [ ^ ]
visit here...


http://msdn.microsoft.com/en-us/library/ms178581.aspx[^]

or

http://stackoverflow.com/questions/8049341/how-pass-session-variables-between-two-or-more-aspx-pages[^]


v_mart不应该是静态的,应该是是page_load方法中的局部变量



在Page_load中进行以下更改



v_mart should not be static and should be a local variable inside page_load method

Make below change in Page_load

int v_Mycart =0;
if (!IsPostBack)
   {
      if(Session["mycart"] !=  null)
        {
            v_MyCart = (int)Session["mycart"];
            
        }

        LblMyCart.Text = Convert.ToString(v_MyCart);
        LblMyCart.Visible = true;
   }


20分钟后,您收到对象引用错误,因为您的会话已过期。对于Session [mycart]会话,不是使用变量v_MyCart变量中的值分配会话,而是直接为会话变量赋值,一切都将顺利进行。
After 20 min you are getting object reference error because your session is getting expired. For Session["mycart"] session, instead of assigning session with value from variable v_MyCart variable, directly assign value to the session variable and everything will work smoothly.


这篇关于如何在所有页面中使用变量的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆