Is there Page_PreInit, but for the entire application?

Discussion in 'ASP .Net' started by Edward, Oct 17, 2006.

  1. Edward

    Edward Guest

    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
    Edward, Oct 17, 2006
    #1
    1. Advertising

  2. Edward

    Mark Rae Guest

    "Edward" <> wrote in message
    news:lZ2Zg.48718$...

    > 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.
    Mark Rae, Oct 17, 2006
    #2
    1. Advertising

  3. 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

    --
    http://www.openmymind.net/
    http://www.codebetter.com/


    "Mark Rae" <> wrote in message
    news:...
    > "Edward" <> wrote in message
    > news:lZ2Zg.48718$...
    >
    >> 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.
    >
    Karl Seguin [MVP], Oct 17, 2006
    #3
  4. Edward

    Mark Rae Guest

    "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
    net> wrote in message news:...

    > 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...
    Mark Rae, Oct 17, 2006
    #4
  5. 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.




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "Edward" <> wrote in message
    news:lZ2Zg.48718$...
    > 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
    >
    >
    Juan T. Llibre, Oct 17, 2006
    #5
  6. I sent in a suggestion before I read both of yours, which also do the work.

    Brock's sample HttpModule code also works...




    Juan T. Llibre, asp.net MVP
    asp.net faq : http://asp.net.do/faq/
    foros de asp.net, en español : http://asp.net.do/foros/
    ===================================
    "Mark Rae" <> wrote in message news:et$...
    > "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net> wrote in message
    > news:...
    >
    >> 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...
    >
    Juan T. Llibre, Oct 17, 2006
    #6
  7. Edward

    Mark Rae Guest

    "Juan T. Llibre" <> wrote in message
    news:eD%...

    >I sent in a suggestion before I read both of yours


    I believe you...:)
    Mark Rae, Oct 17, 2006
    #7
  8. Edward

    Edward Guest

    great! ... so many options to digest!

    thanks everyone for your contribution


    "Edward" <> wrote in message
    news:lZ2Zg.48718$...
    > 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
    >
    >
    Edward, Oct 17, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lammert

    Get Value Dropdownlist Page_PreInit

    Lammert, Jun 26, 2006, in forum: ASP .Net
    Replies:
    9
    Views:
    6,629
    Mark Rae
    Jun 26, 2006
  2. ad
    Replies:
    1
    Views:
    18,051
    =?Utf-8?B?TXVoYW1tYWQgTW9zYQ==?=
    Aug 5, 2006
  3. Page_PreInit

    , Sep 27, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    1,031
    Lloyd Sheen
    Sep 27, 2007
  4. Redhairs
    Replies:
    19
    Views:
    2,967
    Redhairs
    Jan 22, 2008
  5. Vin
    Replies:
    9
    Views:
    5,016
    nayak
    Jul 14, 2009
Loading...

Share This Page