tags:

views:

93

answers:

6

I need to create a code that reads the QueryString and set a value on the Session and on the end of the page I need to clear the Session.

How can I make a code like this to run on all .aspx pages?

+1  A: 

By putting the code in a basepage and letting all your .aspx pages inherit from the basepage.

fARcRY
+8  A: 

Two possibilities are:

  • Create a class that inherits from System.Web.UI.Page. Insert your code there and all your pages inherit from that class instead of System.Web.UI.Page.
  • Create a HttpModule
citronas
an HttpModule is perfect for this.
dave thieben
+2  A: 

Create a class that inherit from Page that you will use instead of Page.

Alternatively, you can use a MasterPage if your application design allows that.

Pierre 303
+4  A: 

well as i see it you got 2 solutions

  1. Use a master page and do it there

  2. Inherit Page and use that as base class on all your pages

One question why must it be stored in a session? this will give your problems if the same user executes 2 pages at the same time (the first to finish will clear the sesson for the other page)

if you only need the data while the page runs you can just save it in a normal variable, else use the viewState!

Petoj
A: 

An easy way to include code that is part of all pages is to subclass Windows.Web.UI.Page, and have all your pages inherit from this new class, instead of Windows.Web.UI.Page. The new class can register for page events, independently from each individual page.

Another option, if you don't want it to be part of each page, and you want to ensure that it runs even if a developer doesn't inherit your new page subclass, is to write an HTTPModule. This hooks into the ASP.NET processing pipeline, and you can trigger off pipeline events, such as authentication or displaying pages. You can write an HTTPHandler and run it out of the pipeline as well. (All pages implement IHTTPHandler, somewhere up the chain.)

Cylon Cat
+4  A: 

Or just use Global.asax: http://en.wikipedia.org/wiki/Global.asax

Kirk Woll
Which event would you handle?
Steven Sudit
Application_BeginRequest: Fires each time a new request comes in.Application_EndRequest: Fires when the request is over.
Kirk Woll