Application_BeginRequest and the Page object

Discussion in 'ASP .Net' started by Mark Rae, Aug 18, 2006.

  1. Mark Rae

    Mark Rae Guest

    Hi,

    Is it possible to have programmatic access to the Page object in
    Application_BeginRequest, or is it too early in the lifecycle...?

    E.g. to be able to change a page's MasterPage dynamically, something like:

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    if (Session["LoggedOn"] == "True")
    {
    <page>.MasterPageFile = "~/loggedOn.master";
    }
    else
    {
    <page>.MasterPageFile = "~/notLoggedOn.master";
    }
    }

    N.B. I realise it's possible to achieve the above functionality in other
    ways - it's just a hypothetical example...

    Any assistance gratefully received.

    Mark
    Mark Rae, Aug 18, 2006
    #1
    1. Advertising

  2. re:
    > Is it possible to have programmatic access to the Page object in Application_BeginRequest, or is
    > it too early in the lifecycle...?


    The problem is that the Application_BeginRequest event is raised for *all* requests.

    Are you sure you want that code to execute every time your application receives a request?

    Check out the Application Lifecycle Overview at :
    http://msdn2.microsoft.com/en-us/library/ms178473.aspx

    and the ASP.NET Page Life Cycle Overview, at :
    http://msdn2.microsoft.com/en-us/library/ms178472.aspx

    I'm sure you'll find a more appropiate application, or page,
    event for that code in one of those two pages.




    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    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:%...
    > Hi,
    >
    > Is it possible to have programmatic access to the Page object in Application_BeginRequest, or is
    > it too early in the lifecycle...?
    >
    > E.g. to be able to change a page's MasterPage dynamically, something like:
    >
    > protected void Application_BeginRequest(Object sender, EventArgs e)
    > {
    > if (Session["LoggedOn"] == "True")
    > {
    > <page>.MasterPageFile = "~/loggedOn.master";
    > }
    > else
    > {
    > <page>.MasterPageFile = "~/notLoggedOn.master";
    > }
    > }
    >
    > N.B. I realise it's possible to achieve the above functionality in other ways - it's just a
    > hypothetical example...
    >
    > Any assistance gratefully received.
    >
    > Mark


    >
    Juan T. Llibre, Aug 18, 2006
    #2
    1. Advertising

  3. Mark,
    Scott Allen has a very in-depth piece all about neat tricks with MasterPages:

    http://www.odetocode.com/Articles/450.aspx

    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "Mark Rae" wrote:

    > Hi,
    >
    > Is it possible to have programmatic access to the Page object in
    > Application_BeginRequest, or is it too early in the lifecycle...?
    >
    > E.g. to be able to change a page's MasterPage dynamically, something like:
    >
    > protected void Application_BeginRequest(Object sender, EventArgs e)
    > {
    > if (Session["LoggedOn"] == "True")
    > {
    > <page>.MasterPageFile = "~/loggedOn.master";
    > }
    > else
    > {
    > <page>.MasterPageFile = "~/notLoggedOn.master";
    > }
    > }
    >
    > N.B. I realise it's possible to achieve the above functionality in other
    > ways - it's just a hypothetical example...
    >
    > Any assistance gratefully received.
    >
    > Mark
    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Aug 18, 2006
    #3
  4. Mark Rae

    Mark Rae Guest

    "Juan T. Llibre" <> wrote in message
    news:%23$vCm$...

    > The problem is that the Application_BeginRequest event is raised for *all*
    > requests.


    That's right.

    > Are you sure you want that code to execute every time your application
    > receives a request?


    Who knows - was just interested to know, hence the "hypothetical" bit... :)

    Is it possible?
    Mark Rae, Aug 18, 2006
    #4
  5. Mark Rae

    Mark Rae Guest

    "Peter Bromberg [C# MVP]" <> wrote in message
    news:...

    Peter,

    > Scott Allen has a very in-depth piece all about neat tricks with
    > MasterPages:


    I was merely using MasterPages as a hypothetical example, hence the sentence
    "N.B. I realise it's possible to achieve the above functionality in other
    ways - it's just a hypothetical example..."

    Do you know if it's possible to have programmatical access to the Page
    object in Application_BeginRequest NOT NECESSARILY for anything related to
    MasterPages...?
    Mark Rae, Aug 18, 2006
    #5
  6. re:
    > Is it possible?


    Yes it is possible
    That code will execute every time your application receives a request.



    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    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:...
    > "Juan T. Llibre" <> wrote in message
    > news:%23$vCm$...
    >
    >> The problem is that the Application_BeginRequest event is raised for *all* requests.

    >
    > That's right.
    >
    >> Are you sure you want that code to execute every time your application receives a request?

    >
    > Who knows - was just interested to know, hence the "hypothetical" bit... :)
    >
    > Is it possible?
    >
    Juan T. Llibre, Aug 18, 2006
    #6
  7. Mark Rae

    Mark Rae Guest

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

    > Yes it is possible


    Cool.

    > That code will execute every time your application receives a request.


    Understood.

    Can you please tell me how to reference the Page object in
    Application_BeginRequest. E.g.

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    if (Session["LoggedOn"] == "True")
    {
    <page>.<property> = <someValue>;
    }
    else
    {
    <page>.<property> = <someOtherValue>;
    }
    }
    Mark Rae, Aug 18, 2006
    #7
  8. re:
    > Can you please tell me how to reference the Page object in Application_BeginRequest. E.g.


    Do you mean as in :

    If Page.IsPostBack

    ?

    Try it...

    Again, the basic problem is that that code code execute for *all* requests.
    I'm not sure that you want Page properties code executed all the time but if you do...




    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    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:%...
    > "Juan T. Llibre" <> wrote in message
    > news:%...
    >
    >> Yes it is possible

    >
    > Cool.
    >
    >> That code will execute every time your application receives a request.

    >
    > Understood.
    >
    > Can you please tell me how to reference the Page object in Application_BeginRequest. E.g.
    >
    > protected void Application_BeginRequest(Object sender, EventArgs e)
    > {
    > if (Session["LoggedOn"] == "True")
    > {
    > <page>.<property> = <someValue>;
    > }
    > else
    > {
    > <page>.<property> = <someOtherValue>;
    > }
    > }
    >
    Juan T. Llibre, Aug 18, 2006
    #8
  9. Mark Rae

    Kevin Jones Guest

    Try

    Page p = Context.Handler as Page;

    if(p != null)
    {
    // use Page here
    }

    Kevin Jones

    Mark Rae wrote:
    > "Peter Bromberg [C# MVP]" <> wrote in message
    > news:...
    >
    > Peter,
    >
    >> Scott Allen has a very in-depth piece all about neat tricks with
    >> MasterPages:

    >
    > I was merely using MasterPages as a hypothetical example, hence the sentence
    > "N.B. I realise it's possible to achieve the above functionality in other
    > ways - it's just a hypothetical example..."
    >
    > Do you know if it's possible to have programmatical access to the Page
    > object in Application_BeginRequest NOT NECESSARILY for anything related to
    > MasterPages...?
    >
    >
    Kevin Jones, Aug 18, 2006
    #9
  10. Mark Rae

    Mark Rae Guest

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

    > Again, the basic problem is that that code code execute for *all*
    > requests.


    I understand that - and that will be my problem, not yours...

    > I'm not sure that you want Page properties code executed all the time but
    > if you do...


    Juan, you're not an unintelligent man, so you are obviously just "playing
    dumb" with me here... That's fine - I've no problem with a little ribbing -
    do it myself more often than not...

    But, you're an MVP, and you know *perfectly well* what I'm asking, so would
    it be possible for you to actually tell me the answer now...?

    However, just to reiterate, I'm looking for a way, a process, a whatever to
    reference the Page object from within the Application_BeginRequest method of
    Global.aspx.cs

    I know that this will execute for *all* requests - I almost certainly won't
    implement it - I ask merely for my own interest and to further my understand
    of ASP.NET.

    You've already told me that it is possible, i.e. you know how to do it and I
    don't.

    So, once again, can you please tell me how to reference the Page object from
    within Application_BeginRequest? Just so there is no ambiguity, I'm
    specifically looking for the namespace / object(s) to take the place of the
    <page> token in the pseudo-code below:

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    <page>.<property> = <someValue>;
    }

    Can you please tell me what it is? I'd be really grateful.
    Mark Rae, Aug 18, 2006
    #10
  11. Mark Rae

    Mark Rae Guest

    "Kevin Jones" <> wrote in message
    news:O4psU%...

    Hi Kevin,

    > Page p = Context.Handler as Page;
    >
    > if(p != null)
    > {
    > // use Page here
    > }


    Thanks for that - problem is that p is always null...

    Mark
    Mark Rae, Aug 18, 2006
    #11
  12. re:
    > Can you please tell me what it is? I'd be really grateful.


    Kevin beat me to it....

    Try

    Page p = Context.Handler as Page;

    if(p != null)
    {
    // use Page here
    }

    Sorry if you are miffed that I was attempting to dissuade you from
    using that type of code but I, honestly, don't see much use in using it.

    Maybe you could explain to me why it is important to you ?

    i.e., can you tell me why detecting Page properties in
    Application_BeginRequest won't result in superfluous code




    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    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:%...
    > "Juan T. Llibre" <> wrote in message
    > news:...
    >
    >> Again, the basic problem is that that code code execute for *all* requests.

    >
    > I understand that - and that will be my problem, not yours...
    >
    >> I'm not sure that you want Page properties code executed all the time but if you do...

    >
    > Juan, you're not an unintelligent man, so you are obviously just "playing dumb" with me here...
    > That's fine - I've no problem with a little ribbing - do it myself more often than not...
    >
    > But, you're an MVP, and you know *perfectly well* what I'm asking, so would it be possible for you
    > to actually tell me the answer now...?
    >
    > However, just to reiterate, I'm looking for a way, a process, a whatever to reference the Page
    > object from within the Application_BeginRequest method of Global.aspx.cs
    >
    > I know that this will execute for *all* requests - I almost certainly won't implement it - I ask
    > merely for my own interest and to further my understand of ASP.NET.
    >
    > You've already told me that it is possible, i.e. you know how to do it and I don't.
    >
    > So, once again, can you please tell me how to reference the Page object from within
    > Application_BeginRequest? Just so there is no ambiguity, I'm specifically looking for the
    > namespace / object(s) to take the place of the <page> token in the pseudo-code below:
    >
    > protected void Application_BeginRequest(Object sender, EventArgs e)
    > {
    > <page>.<property> = <someValue>;
    > }
    >
    > Can you please tell me what it is? I'd be really grateful.
    Juan T. Llibre, Aug 18, 2006
    #12
  13. Mark Rae

    Kevin Jones Guest

    The value of p will depend on the type of content you are requesting.
    Context.Handler contains a reference to the IHttpHandler that is
    handling the request, if you send a request to a .aspx page then the
    IHttpHandler will be the page you created so p shouldn't be null.

    Try putting a break point on the Context.Handler as Page line and see
    what type it is,

    Kevin
    Kevin Jones, Aug 18, 2006
    #13
  14. You might want to consider using Page_PreInit or Page_Init to execute code
    which affects a particular page, instead of using Application_BeginRequest, which affects all pages.

    There's other Page events which you might want
    to consider as places where to execute Page code events.

    For a complete list, see : http://msdn2.microsoft.com/en-us/library/ms178472.aspx



    Juan T. Llibre, asp.net MVP
    aspnetfaq.com : http://www.aspnetfaq.com/
    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:...
    > "Kevin Jones" <> wrote in message
    > news:O4psU%...
    >
    > Hi Kevin,
    >
    >> Page p = Context.Handler as Page;
    >>
    >> if(p != null)
    >> {
    >> // use Page here
    >> }

    >
    > Thanks for that - problem is that p is always null...
    >
    > Mark
    >
    Juan T. Llibre, Aug 18, 2006
    #14
  15. Mark Rae

    Mark Rae Guest

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

    > Maybe you could explain to me why it is important to you ?


    Interest. Learning.

    > i.e., can you tell me why detecting Page properties in
    > Application_BeginRequest won't result in superfluous code


    I'm certain that it would...
    Mark Rae, Aug 18, 2006
    #15
  16. Mark Rae

    Mark Rae Guest

    "Kevin Jones" <> wrote in message
    news:%...

    > The value of p will depend on the type of content you are requesting.
    > Context.Handler contains a reference to the IHttpHandler that is handling
    > the request, if you send a request to a .aspx page then the IHttpHandler
    > will be the page you created so p shouldn't be null.


    It always is...

    > Try putting a break point on the Context.Handler as Page line and see what
    > type it is,


    It's always null... In the Immediate Window, if I ask it to display
    Context.Handler.GetType(), the result is always:

    '((object)(((System.Web.HttpApplication)(this)).Context.Handler))' is null
    Mark Rae, Aug 18, 2006
    #16
  17. Mark,
    sorry about getting thrown off by the reference to Master pages. The bottom
    line:
    Application_BeginRequest is fired at the very, VERY beginning of the entire
    request processing pipeline, way before any instance of the Page class is
    created.
    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "Mark Rae" wrote:

    > "Kevin Jones" <> wrote in message
    > news:O4psU%...
    >
    > Hi Kevin,
    >
    > > Page p = Context.Handler as Page;
    > >
    > > if(p != null)
    > > {
    > > // use Page here
    > > }

    >
    > Thanks for that - problem is that p is always null...
    >
    > Mark
    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Aug 18, 2006
    #17
  18. Mark Rae

    Mark Rae Guest

    "Peter Bromberg [C# MVP]" <> wrote in message
    news:...

    Peter,

    > sorry about getting thrown off by the reference to Master pages.


    S'OK - I now see it was my fault for not phrasing my question more
    clearly...

    > The bottom line:
    > Application_BeginRequest is fired at the very, VERY beginning of the
    > entire
    > request processing pipeline, way before any instance of the Page class is
    > created.


    Hence, the very first line of my original post...

    "Is it possible to have programmatic access to the Page object in
    Application_BeginRequest, or is it too early in the lifecycle...?"

    So, is it or isn't it? Is this why the code suggested by Kevin always
    returns the Page object as null?

    Mark
    Mark Rae, Aug 18, 2006
    #18
  19. Mark Rae

    Kevin Jones Guest

    >> The bottom line:
    >> Application_BeginRequest is fired at the very, VERY beginning of the
    >> entire
    >> request processing pipeline, way before any instance of the Page

    class is
    >> created.



    > So, is it or isn't it? Is this why the code suggested by Kevin always
    > returns the Page object as null?


    Yep, sorry. The Handler doesn't exist and so is not set in the context
    until the PreRequestHandlerExecute event (which is where I've always
    done this in my code)

    Kevin
    Kevin Jones, Aug 18, 2006
    #19
  20. Mark Rae

    Mark Rae Guest

    "Kevin Jones" <> wrote in message
    news:%...

    > Yep, sorry. The Handler doesn't exist and so is not set in the context
    > until the PreRequestHandlerExecute event (which is where I've always done
    > this in my code)


    Well that pretty much answers my original question - thanks.
    Mark Rae, Aug 18, 2006
    #20
    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. Franck
    Replies:
    1
    Views:
    476
    Karl Seguin
    Oct 4, 2005
  2. Wayne Sepega
    Replies:
    1
    Views:
    2,179
    Wayne Sepega
    Jan 6, 2006
  3. tao lin
    Replies:
    1
    Views:
    3,767
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Mar 26, 2006
  4. Guest
    Replies:
    1
    Views:
    152
    Brock Allen
    May 25, 2005
  5. Franck
    Replies:
    0
    Views:
    141
    Franck
    Sep 30, 2005
Loading...

Share This Page