Is there Page_PreInit, but for the entire application?

E

Edward

Hi All,

Would anyone know if there is an event similar to the Page_PreInit that I
can code just once, at a global initialization level, that applies to all
pages executed?

Therefore, instead of placing the code below in every page codebehind, I
could code it just once.
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit

If Not Request.Browser.Browser.Contains("IE") Then

Me.ClientTarget = "uplevel"

End If

End Sub


Thanks

Edward Re
 
M

Mark Rae

Would anyone know if there is an event similar to the Page_PreInit that I
can code just once, at a global initialization level, that applies to all
pages executed?

There isn't.
Therefore, instead of placing the code below in every page codebehind, I
could code it just once.
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.PreInit

If Not Request.Browser.Browser.Contains("IE") Then

Me.ClientTarget = "uplevel"

End If

End Sub

Two main options:

1) Use MasterPages and ContentPages. However, since MasterPages don't have a
Page_PreInit event, you would need to create a base class which inherited
the Page object, create a Page_PreInit event in that, and then have your
MasterPages inherit the base class.

2) Do the above but without MasterPages i.e. create a base class with the
Page_PreInit code, and derive all your other pages from it.
 
K

Karl Seguin [MVP]

The master page solution is a good one.

an HttpModule's BeginRequest might also do the trick (or you could just use
Global.asax's).

Karl
 
M

Mark Rae

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message
The master page solution is a good one.

That's the one I use.
an HttpModule's BeginRequest might also do the trick (or you could just
use Global.asax's).

Here's an article which describes this, albeit for a different purpose...
http://asp-net-whidbey.blogspot.com/2006/02/url-rewriting-using-aspnet-20-http.html

Interestingly enough, I've never actually got this to work properly - IIRC,
although the BeginRequest method does indeed fire before the PreInit method,
I never seemed to get access to the Page object at this stage...
 
J

Juan T. Llibre

re:
Would anyone know if there is an event similar to the Page_PreInit that I can code just once, at a
global initialization level, that applies to all pages executed?

There isn't one.
The good news is that you can build an httpmodule which will do the job for you.

See :
http://staff.develop.com/ballen/blog/PermaLink.aspx?guid=09befce7-f48e-4555-891c-13818fd75a56

Brock uses it to select a Theme, but you can modify the code to do what you want to do.

His sample code is at :
http://staff.develop.com/ballen/blo...lop.com/ballen/samples/DynamicThemeSample.zip

....but read his blog entry before attempting to use it.
 
J

Juan T. Llibre

I sent in a suggestion before I read both of yours, which also do the work.

Brock's sample HttpModule code also works...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top