哪个是将静态变量更改为会话的更好方法? [英] Which is the better way to change the static variables into sessions?

查看:77
本文介绍了哪个是将静态变量更改为会话的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个小型Web应用程序。在我的应用程序中,我使用静态变量来全局存储数据。我正面临着静态变量的问题。我在两个不同的浏览器中打开我的应用程序。我有 这里的问题,静态变量为不同的浏览器全局存储值。我该如何解决这个问题。有没有更好的方法将静态变量转换为会话。

I have implemented one small web application. In my application i have used the static variables to store the data globally. I am facing the problem with the static variables. I am opening my application in two different browsers. I got the problem here the static variables are storing the value globally for different browsers. How can i solve this issue. Is there any better way to convert the static variables to the sessions.

推荐答案

你好Cbab,



我认为以下示例代码可以帮助您。



Hi Cbab,

I think the following sample code helps you.

//create a session variable and store the value you need to(Any datatype can be stored)
Session["sessionname"] = TheValueToBeStored;

//extract the value from the session to the variable of the corresponding type.
short Var_name= Convert.ToInt16(Session["sessionname"]);





谢谢....

Sijo



Thank you....
Sijo


我将静态变量修改为普通变量,并将变量存储到会话中。如果我们想获取变量的数据,我们可以从会话中获取数据。



注意:

会话正在存储变量的地址。如果我们自动更改变量的值,会话的值也会发生变化。
I modified static variable to normal variable, and i stored the variable into the session. If we want to get the data of the variable we can get the data from the session.

Note:
The session is storing the address of the variable. If we change the value of the variable automatically the value of the session is also change.


查看问题陈述,看起来你需要一些变量来表现,好像它们是静态的应用视角。将变量更改为会话变量将允许您重用同一会话中的变量。如果您需要从所有应用程序中获取变量值,那么您将需要使用Application object./code>object。

Looking at the problem statement it looks like you need some variables to behave as if they are static from your applications perspective. Changing the variables to session variables will let you reuse the variable from same session. If you need to make the variable value available from all your application then you will need to use Application object./code>object.
Application["key"] = value;



这里有一个小链接,可以理解这背后的逻辑。



初学者的ASP.NET状态管理教程 [ ^ ]



如果我理解错误,请告诉我,我会尽力改进我的答案。


here is a small link to understand the logic behind doing this.

A Beginner''s Tutorial on ASP.NET State Management[^]

Let me know if I understood it incorrectly and I will try to refine my answer.


这篇关于哪个是将静态变量更改为会话的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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