如何将值永久存储在变量中? [英] How can I store a value in a variable permanently?

查看:118
本文介绍了如何将值永久存储在变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我在一个类中,并且从另一个类访问一个varibal,但是我只能在此类加载页面时访问该变量.一旦页面加载完成,变量中的值就消失了.但是我需要永久存储该变量的值,以便即使在页面加载完成后也需要使用该值进行一些活动.

我的问题是如何在页面加载时存储会话状态变量的值,以便即使页面加载完成也可以使用该值.

如果可以的话,我可以获取完整的代码吗?我是新手.

解决方案

您说的是页面加载,所以我假设您在ASP.Net中工作吗?

如果它只是一个小变量,例如字符串或整数,请考虑将值存储在用户中"会话 [^ ].然后,您可以在以后需要使用该会话时从会话中检索该值

如果我的问题正确,那么您希望在第一页加载时设置该值,然后在按钮上进行访问回发邮件.在page_load()中使用以下内容:

 如果(!IsPostBack)
   会话(" )=  ; 



单击按钮后,您可以获取已设置的值,如下所示:

 字符串 val =会话(" ).ToString(); 




好吧,我不确定我是否了解.

您描述的内容似乎是不可能的,您能告诉我们您的课程的结构如何吗?

第一类如何要求存储在另一类中的值?是财产吗?

仅仅是变量"变得丢失"还是类变为空?

有几种存储数据的方式(缓存,会话,页面项,Viewstate等),但是您不应该存储不需要存储的内容.在这种情况下,我认为问题出在其他地方.


I have a case where I''m in a class and accessing a varibal from a different class but I could only access the varible on page load of this class. Once the page load is complete the value in the variable is gone. But i need to store the varible''s value permanatly so that even after the page load is complete I need to use the value to do some activity.

My question is how to store the session state varible''s value on page load, so that I may be able to use the value eventhough the page load is finished.

Can I get a complete code if possible please? I''m new to it.

解决方案

You say page load so I''m assuming you''re working in ASP.Net?

If it''s just a small variable, such as a string or integer, consider storing the value in the users'' session[^]. You can then retrieve the value from the session when you need to use it later


If I get the question correct, you want to set the value on 1st page load and then access it on button clikc in postback. Use the below in page_load():

if(!IsPostBack)
   Session("myval")=value;



On button click, you can get the value you have set as below:

string val = Session("myval").ToString();


Hi,

Well, I''m not sure I understand.

What you describe seems impossible, could you tell us how are your classes structured?

How does the first class asks for the value stored in the other class? Is it a property?

Is it only the "variable" that gets "lost" or the class becomes null?

There are several ways to store data, (Cache, Session, Page items, Viewstate, etc.), but you shouldn''t store what''s not meant to. In this case, I think the problem is elsewhere.


这篇关于如何将值永久存储在变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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