Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Application_BeginRequest and the Page object (http://www.velocityreviews.com/forums/t365147-application_beginrequest-and-the-page-object.html)

Mark Rae 08-18-2006 01:58 PM

Application_BeginRequest and the Page object
 
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 08-18-2006 02:09 PM

Re: Application_BeginRequest and the Page object
 
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" <mark@markNOSPAMrae.com> wrote in message news:%23lhJv5swGHA.2120@TK2MSFTNGP03.phx.gbl...
> 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=?= 08-18-2006 02:25 PM

RE: Application_BeginRequest and the Page object
 
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
>
>
>


Mark Rae 08-18-2006 02:52 PM

Re: Application_BeginRequest and the Page object
 
"Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
news:%23$vCm$swGHA.380@TK2MSFTNGP05.phx.gbl...

> 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 08-18-2006 02:54 PM

Re: Application_BeginRequest and the Page object
 
"Peter Bromberg [C# MVP]" <pbromberg@yahoo.nospammin.com> wrote in message
news:F757CE5C-B0A0-4A7F-A23B-6D4E9F0A64C9@microsoft.com...

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



Juan T. Llibre 08-18-2006 02:59 PM

Re: Application_BeginRequest and the Page object
 
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" <mark@markNOSPAMrae.com> wrote in message news:uBso0XtwGHA.324@TK2MSFTNGP06.phx.gbl...
> "Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
> news:%23$vCm$swGHA.380@TK2MSFTNGP05.phx.gbl...
>
>> 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 08-18-2006 03:15 PM

Re: Application_BeginRequest and the Page object
 
"Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
news:%23DSunbtwGHA.1296@TK2MSFTNGP02.phx.gbl...

> 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 08-18-2006 03:48 PM

Re: Application_BeginRequest and the Page object
 
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" <mark@markNOSPAMrae.com> wrote in message news:%23bXGWktwGHA.1216@TK2MSFTNGP03.phx.gbl...
> "Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
> news:%23DSunbtwGHA.1296@TK2MSFTNGP02.phx.gbl...
>
>> 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>;
> }
> }
>




Kevin Jones 08-18-2006 04:01 PM

Re: Application_BeginRequest and the Page object
 
Try

Page p = Context.Handler as Page;

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

Kevin Jones

Mark Rae wrote:
> "Peter Bromberg [C# MVP]" <pbromberg@yahoo.nospammin.com> wrote in message
> news:F757CE5C-B0A0-4A7F-A23B-6D4E9F0A64C9@microsoft.com...
>
> 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 08-18-2006 04:07 PM

Re: Application_BeginRequest and the Page object
 
"Juan T. Llibre" <nomailreplies@nowhere.com> wrote in message
news:uiFgD3twGHA.4876@TK2MSFTNGP04.phx.gbl...

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




All times are GMT. The time now is 10:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.