PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Application_BeginRequest and the Page object

 
 
Mark Rae
Guest
Posts: n/a
 
      18th Aug 2006
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


 
Reply With Quote
 
 
 
 
Juan T. Llibre
Guest
Posts: n/a
 
      18th Aug 2006
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" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
> 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


>



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      18th Aug 2006
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
>
>
>

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      18th Aug 2006
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:%23$vCm$(E-Mail Removed)...

> 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?


 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      18th Aug 2006
"Peter Bromberg [C# MVP]" <(E-Mail Removed)> wrote in message
news:F757CE5C-B0A0-4A7F-A23B-(E-Mail Removed)...

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


 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      18th Aug 2006
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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> "Juan T. Llibre" <(E-Mail Removed)> wrote in message
> news:%23$vCm$(E-Mail Removed)...
>
>> 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?
>



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      18th Aug 2006
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

> 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>;
}
}


 
Reply With Quote
 
Juan T. Llibre
Guest
Posts: n/a
 
      18th Aug 2006
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" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
> "Juan T. Llibre" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
>> 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>;
> }
> }
>



 
Reply With Quote
 
Kevin Jones
Guest
Posts: n/a
 
      18th Aug 2006
Try

Page p = Context.Handler as Page;

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

Kevin Jones

Mark Rae wrote:
> "Peter Bromberg [C# MVP]" <(E-Mail Removed)> wrote in message
> news:F757CE5C-B0A0-4A7F-A23B-(E-Mail Removed)...
>
> 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...?
>
>


 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      18th Aug 2006
"Juan T. Llibre" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Application_BeginRequest issue Joe Microsoft ASP .NET 7 22nd Apr 2008 04:54 AM
Alternatives to Application_BeginRequest() musosdev Microsoft ASP .NET 3 6th Dec 2007 02:57 AM
Application_BeginRequest does not seem to run =?Utf-8?B?TWlrZSBPd2Vu?= Microsoft Dot NET 3 4th Jul 2006 12:12 PM
Application_BeginRequest questions JezB Microsoft ASP .NET 3 6th Jul 2004 05:27 PM
Using CommandArgument in Application_BeginRequest Amil Microsoft ASP .NET 1 19th Feb 2004 11:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:16 PM.