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"?
Try this after navigate to page
or to completely remove the session
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
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:
One way would be to use the
global.asax
file of the web-application and it'sApplication_BeginRequest
event:However, i would let it die silently when the session exprires.
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.