C# 我应该在 asp.net 中声明会话变量的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2344636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:39:20  来源:igfitidea点击:

Where I should declare a session variable in asp.net

c#.netasp.netsessionsession-variables

提问by Vaibhav Jain

I am building a Asp.net Application. I need to save a HashTable in a session.

我正在构建一个 Asp.net 应用程序。我需要在会话中保存一个 HashTable。

At page load i am writing

在页面加载时我正在写

 protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       Session["AttemptCount"]=new Hashtable(); //Because of this line.
    }   
}

Here problem is, when a user refresh the page, session["AttemptCount"] also get refreshed. I want to know where should I declare

这里的问题是,当用户刷新页面时, session["AttemptCount"] 也会刷新。我想知道我应该在哪里申报

Session["AttemptCount"]=new Hashtable();

So that my seesion do not get refeshed.

这样我的视力就不会被刷新。

EDITIn 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

编辑在 Global.asax 中,一旦用户打开网站,此会话就会开始。我只想在用户转到特定页面时创建此会话。即登录.aspx

采纳答案by Naeem Sarfraz

Do it in the Session_Startmethod in your Global.asaxlike so...

Session_Start在你的Global.asax的方法中这样做......

protected void Session_Start(object sender, EventArgs e)
{
    Session["AttemptCount"]=new Hashtable();
}

Update:

更新:

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"];
    }
}

And then you could just call on the property AttemptCountwherever you need like so...

然后你可以AttemptCount像这样在任何你需要的地方打电话给财产......

public void doEvent(object sender, EventArgs e)
{
    AttemptCount.Add("Key1", "Value1");
}

回答by Ryan O'Neill

Look at Global.asax and the Application_Started (I think) and there is one for session started too.

查看 Global.asax 和 Application_Started(我认为),也有一个用于会话启动。

回答by Pharabus

test if it exists first

首先测试它是否存在

 protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       if(Session["AttemptCount"] == null)
       {
          Session["AttemptCount"]=new Hashtable(); //Because of this line.
       }
    }   
}

though the session_start is better, you only need to uses it on one page but you can create it for each session.

尽管 session_start 更好,但您只需在一页上使用它,但您可以为每个会话创建它。

回答by M4N

You could make a property like this in your page:

您可以在页面中创建这样的属性:

protected Hashtable AttemptCount
{
  get
  {
    if (Session["AttemptCount"] == null)
      Session["AttemptCount"] = new Hashtable();
    return Session["AttemptCount"] as Hashtable; 
  }
}

then you can use it without having to worry:

那么您就可以使用它而不必担心:

protected void Page_Load(object sender, EventArgs e)
{
  this.AttemptCount.Add("key", "value");
}

回答by Shiv Shankar Mahla

Hashtable hastable_name=new Hashtable()
Session["AttemptCount"]=hastable_name