i want the script to clear the value of a static variable once user leaves that particular page
hi,
i am using AsP.net C#
i want the script to clear the value of a static variable once user leaves that particular page.
by leaving that particular page i mean he either closes browser, navigate to another page, but not post back like postback from a button that brings the user back to the same page.
help
thanks
What do you mean by "static"? It's not a variable declared with the static (VB=Shared) keyword, is it? If it is, all the users would write on the same copy of the variable, and so you wouldn't need to clear its value...
So I take that by "static" you mean a Session variable. Well...
- You don't need to worry if the user closes the form or navigates outside your site: the session will time out and the variable will be erased. If the value of the variable is stored on disk or DBs, you can put the code to clear it in the Session_End() method in Global.asax.
- If the user navigates to other pages in your site, you take 2 approaches:
* Check the variable status in the Page_Load() method of every page that could be affected by that variable and clear it if needed. Alternatively, clear it in the Page_Load() of ALL paged but the one using it.
* Add a JavaScript event handler for the unload event on the client. Such handler should open a hidden window (a pop-under) that calls your page with certain parameters (for example, a concrete querystring) to clear the variable value. Then, the pop-under calls a method on its parent to continue the unload and closes itself. Of course, the unload event handler must check that the destination of the new call is not itself (postbacks), so this implementation may be a bit clumsy.
Anyway... why the hell do you need to clear the value of a Session variable? I can't spot a possible reason: just give it a name that prevents it from interfering with other pages.