user249375
user249375

Reputation:

Clearing a session

I have a session class where i am saving text box values(application) in a session. However, when the user times out due to inactivity or just goes back to the home page i want to clear all session data. I tried to set it in the homepage but the !IsPostBack keeps firing.

if (!IsPostBack)
{
    JobPositionSystemDAL jps = new JobPositionSystemDAL();
    DataSet ds = jps.GetJobs();

    GridView2.DataSource = ds;
    GridView2.DataBind();
}

if (IsPostBack)
{
    Session.RemoveAll();
}

Thank you.

Upvotes: 0

Views: 101

Answers (1)

Oded
Oded

Reputation: 499002

The session will automatically clear when it timesout (due to user inactivity, as you put it).

If you want to clear the session whenever the user in on the homepage, just do that. No need to keep checking if it is a postback or not.

When going directly to the homepage (say through a link on another page), IsPostBack is false - this is why !IsPostBack is true and the code within that if block executes.

Upvotes: 3

Related Questions