Andre Hofmeister
Andre Hofmeister

Reputation: 3416

asp.net set sessions before postback

is there an easy way, to store all needed global variables in sessions at once, before the PostBack starts? Or have I to store them in each step where I change them? I will do something like:

// Global variable.
bool test = true;

// Store all needed information in a session.
protected void Before_globalvariable_is_set_to_default_value(...)
{
    Session["Test"] = test;
    ...
}

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
    {
        //if(Session["Test"] != null)
        //{
            test = (bool)Session["Test"];
            Session.Contents.Remove("Test");
        //}
    }
}

Is something like that possible?

Additional Information

At the Page_Load (!IsPostBack) I check if the user gets more vision, if he gets, I set a global var to true. Later in my code I check if that var is true and add additional columns to a GridView. Now if a PostBack occurs, I can’t check that var, because I lose the information. I knew that I need to store the information in a Session. If I set the Session at the time where I set the global var to true, I get problems with the session timeout (If the user is on the site, but doesn’t do something for a while). So I thought it will be good, if I set the Session shortly before I lose the information of the global var and delete the Session after reinitialization.

That’s my idea, but I don’t know if something like that is possible.

Edit2: If I do following it works:

//Global variable
bool test = false;

protected void Page_PreRender(object sender, EventArgs e)
{
    Session["Test"] = test;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        test = (bool)Session["Test"]; // Session is true!!!
        Session.Contents.Remove("Test");
    }
    else
    {
        test = true; // Set at the PageLoad the var to true.
    }
}

I’m a little bit confused, I thought PreRender is after the PageLoad, why suddenly the test var is true and if I remove the PreRender it isn’t?

Greetz

Upvotes: 0

Views: 3664

Answers (2)

Hans Kesting
Hans Kesting

Reputation: 39283

If the value only needs to live during that one Request, you can use class-level fields of the code-behind class. Set them in the Init or Load phase, then you can use those values in all other phases.

For a lifetime of just a single request:

public partial class MyPage: Page
{
   private bool test = true;

   public void Page_Load(...)
   {
      // maybe set 'test' to another value
   }

   public void Button_Click(...)
   {
      // you can still access 'test'
   }

   public void Page_PreRender(...)
   {
      // you can still access 'test'
   }
}

If however you need that value to live from request to the next postback, you can use ViewState instead of Session. Advantage: no timeout as it is stored in the html and posted back from the browser along with other data. Disadvantage: it only works in postback-scanario's, not when you link to a different page.

Upvotes: 1

richaux
richaux

Reputation: 2672

If you're worried about losing a specific value between requests, because you've maintained the state of that variable in the Session object and it might have been cleared by a timeout, you could consider using another, more durable, mechanism to save the state: for example, cookies or database.

Upvotes: 1

Related Questions