Where I should declare a session variable in asp.net

后端 未结 5 634
醉话见心
醉话见心 2021-01-02 14:33

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

At page load i am writing

 protected void Page_Load(object sender, Even         


        
5条回答
  •  温柔的废话
    2021-01-02 15:08

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

提交回复
热议问题