问题描述
我建立一个Asp.net应用。我需要保存一个哈希表中的会话。
I am building a Asp.net Application. I need to save a HashTable in a session.
在页面加载我写
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["AttemptCount"]=new Hashtable(); //Because of this line.
}
}
下面的问题是,当用户刷新页面,会话[AttemptCount]也得到刷新。我想知道我应该声明
Here problem is, when a user refresh the page, session["AttemptCount"] also get refreshed.I want to know where should I declare
Session["AttemptCount"]=new Hashtable();
让我SEESION没有得到refeshed。
So that my seesion do not get refeshed.
修改在Global.asax中,本届会议将开始,为用户打开网站,很快。我想创造这个会议只有当用户进入一个特定的页面。即的Login.aspx
EDIT In Global.asax, this session will get started, as soon as user opens the website. I want to creat this session only if user go to a particular page. i.e Login.aspx
推荐答案
它别在你的像这样...
Do it in the Session_Start
method in your Global.asax like so...
protected void Session_Start(object sender, EventArgs e)
{
Session["AttemptCount"]=new Hashtable();
}
更新:
然后简单的只是做一个检查,看看是否会话变量存在,如果它不只有创建变量。你可以把它贴在属性,使事情更清洁,像这样......
Then simply just do a check to see if the session variable exists, if it doesn't only then create the variable. You could stick it in a property to make things cleaner like so...
public Hashtable AttemptCount
{
get
{
if (Session["AttemptCount"] == null)
Session["AttemptCount"]=new Hashtable();
return Session["AttemptCount"];
}
}
然后你可以只呼吁物业 AttemptCount
只要您需要像这样...
And then you could just call on the property AttemptCount
wherever you need like so...
public void doEvent(object sender, EventArgs e)
{
AttemptCount.Add("Key1", "Value1");
}
这篇关于在那里我应该声明在asp.net会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!