Last post Dec 08, 2008 02:14 PM by imran_ku07
Dec 07, 2008 11:46 AM|seebharath|LINK
I have a webpage in my site where I compulsorily need to perform a task when the user navigates from that page to any other page pf the site..
So i was thinking is there any PageLeave event similar to the PageLoad event that is fired when user navigates from page ???
OR is there any way to make sure that a certain set of lines odf code are always executed when user navigates away from the page..???
Dec 07, 2008 12:18 PM|XIII|LINK
there are several scenarios, and probably even more than what I come up with now, to accomplish this.
1) always postback and use, after executing your lines of code in the page, a response.redirect.
2) (the way I would go for): create a HttpModule and keep track of the current served page. When the next hit on the server (navigating to the same page/postback or requesting a new page to be served) you could compare the current requested page with the
previous one. If they don't match execute your lines of code.
Dec 07, 2008 01:10 PM|XIII|LINK
probably even better:
HttpModule BeginRequest you can capture the information through the HttpApplication Request property:
if(application.Request.Url == application.Request.UrlReferrer)
// metacode just to give you an idea. It could be that you need to refine it a bit to work.
Or you can do it like this in the BeginRequest (based on the formentioned example)
Page p = (Page)context.Handler;
// ... your lines of code
Dec 08, 2008 07:40 AM|seebharath|LINK
I have just started to work on asp.net for a month now and also new to the software industry....So
Could please tell me what is a
HttpModule BeginRequest and BeginRequest ???
Dec 08, 2008 08:04 AM|XIII|LINK
a HttpModule is a component that you can plugin into the execution of a request made to the server. The handy thing about it that you can use it to preprocess and postprocess the request to a httphandler (mostly a webform).
The link behind the Beginrequest shows the evenhandler to which you can hook in to capture the information you need. Also take a look at
Before you start with anything else on this part, read this article: Walkthrough: Creating and Registering a Custom HTTP Module.
Dec 08, 2008 09:13 AM|imran_ku07|LINK
Kriss what about session which contains the URL.
For Example in every Page you can use session to determine which is the previous page.
First time Session is null. Onthe Next Time it contains the URL of Previous Page.
//your code here
Session["URL"]=Request.Url.ToString();//assign session to new URL
you can also use master page to put this code once.
Dec 08, 2008 09:26 AM|XIII|LINK
what about session which contains the URL
This can also be made like that yes. The best way to implement it then is to create a custom class that inherits from Page class and have your webforms inherit from that class. This way you only have to implement the code once.
Personally I'm more for the HttpModule approach because of a specific feature. When you're live and you don't want to include the code anymore you can simply plug it out by setting some configuration in the web.config file in comment.
Dec 08, 2008 02:14 PM|imran_ku07|LINK
Thanks for your good suggestion.