asp.net free session variables on page change

2019-07-21 16:01发布

I have a web application that use Session variables for mantain the state of some datatable. I'm not using viewstate for not increase weight of the page.

I not need to mantain the state on page changing, so I want free these session variables on page leave.

How can I intercept when user changing website page ? There is a method like "Page Leave"?

5条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-21 16:37

Try this after navigate to page

Session.Remove("Name");

or to completely remove the session

Session.Clear();
or 
Session.Abadon();
查看更多
女痞
3楼-- · 2019-07-21 16:38

I suggest you tu use class base for all pages of your website, and use PreviousPage property, who permit you to define your link (state machine diagram)

And you can define Session.Remove for cleaning

Another remark : suggest you to use viewstate because pemit you persist datas without adding code, important to have < 30K for each page. just this

查看更多
▲ chillily
4楼-- · 2019-07-21 16:40

In a web application, there are various ways a user can "leave" a page. The model of a web application is request based; client and server do not know of each other between requests. Some techniques as cookies or session state try to close this gap, but basically there is no connection.
Therefore, the browser will not notify the server if the user leaves a page, so there is no such event as a Page_Leave. However, you could solve this for some cases (e.g. if the user changes to another page of your application), but not for all.
As @Terror.Blade pointed out, session data is for data that needs to be persisted across a single pages lifecycle. Therefore I'd not suggest to put these data into the Session and find a (not very stable) way to delete it once the user leaves the page.
As it seems it is not an option for you to request the DataTable again for the same user (though this is a common way). I can suggest you the following alternatives:

  • If the DataTable is not too big, as you know ViewState is an option that asserts that the data is not persisted across the page lifecycle.
  • You can also use the Cache to store the data. In contrast to Session state, the Cache has capabilities to free the memory after a certain time or if memory is required for more important data. However, be prepared that the data might need to be fetched again if it was removed from the Cache between requests. Also be aware that the cache is shared among all users and not limited to a Session, so you need to make sure that the users do not see information that is not intended for them. This can also be an advantage, if the DataTable is the same for several users, you can store it in the Cache once.
查看更多
对你真心纯属浪费
5楼-- · 2019-07-21 16:42

One way would be to use the global.asax file of the web-application and it's Application_BeginRequest event:

public class Global : System.Web.HttpApplication
{
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext context = base.Context;
        HttpRequest request = context.Request;
        string pageName = System.IO.Path.GetFileNameWithoutExtension(request.RawUrl);
        if (pageName != "YourPageName")
        {
            if(context.Session["NameOfSessionVariable"] != null)
                context.Session.Remove("NameOfSessionVariable");
        }
    }
}

However, i would let it die silently when the session exprires.

查看更多
【Aperson】
6楼-- · 2019-07-21 16:51

Session data is useful for storing data beyond the lifetime of a page. If you don't want to store it beyond the life of a page then Session data is not for you here.

查看更多
登录 后发表回答