473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Aug 18 '06 #1
19 3550
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" <ma**@markNOSPAMrae.comwrote in message news:%2****************@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
>

Aug 18 '06 #2
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
Aug 18 '06 #3
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:%2***************@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?
Aug 18 '06 #4
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:F7**********************************@microsof t.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...?
Aug 18 '06 #5
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" <ma**@markNOSPAMrae.comwrote in message news:uB*************@TK2MSFTNGP06.phx.gbl...
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:%2***************@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?

Aug 18 '06 #6
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:%2****************@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>;
}
}
Aug 18 '06 #7
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" <ma**@markNOSPAMrae.comwrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:%2****************@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>;
}
}

Aug 18 '06 #8
Try

Page p = Context.Handler as Page;

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

Kevin Jones

Mark Rae wrote:
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:F7**********************************@microsof t.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...?

Aug 18 '06 #9
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:ui**************@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
<pagetoken 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.
Aug 18 '06 #10
"Kevin Jones" <kj****@develop.comwrote in message
news:O4****************@TK2MSFTNGP06.phx.gbl...

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
Aug 18 '06 #11
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" <ma**@markNOSPAMrae.comwrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:ui**************@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 <pagetoken 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.

Aug 18 '06 #12
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
Aug 18 '06 #13
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" <ma**@markNOSPAMrae.comwrote in message news:uU*************@TK2MSFTNGP06.phx.gbl...
"Kevin Jones" <kj****@develop.comwrote in message
news:O4****************@TK2MSFTNGP06.phx.gbl...

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

Aug 18 '06 #14
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:eR**************@TK2MSFTNGP02.phx.gbl...
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...
Aug 18 '06 #15
"Kevin Jones" <kj****@develop.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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)).Co ntext.Handler))' is null

Aug 18 '06 #16
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" <kj****@develop.comwrote in message
news:O4****************@TK2MSFTNGP06.phx.gbl...

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
Aug 18 '06 #17
"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:71**********************************@microsof t.com...

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
Aug 18 '06 #18
>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

Aug 18 '06 #19
"Kevin Jones" <kj****@develop.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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.
Aug 18 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Daly (MCP) | last post by:
I'm trying to write a log file that captures the referring url if the request is a new session, and captures a querystring value if the user is browsing between pages on the website. When using...
1
by: JezB | last post by:
1) How can I access my object-oreinted classes from the global.asax Application_BeginRequest event ? I cannot instantiate them from session since it complains : Session state is not available in...
2
by: Stu | last post by:
Hi, I am trying to access session variable value from the 'Application_BeginRequest' handler in the global asax file but get the message - 'Session state not available in this context'. I have...
7
by: Ankit Aneja | last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx some .aspx pages are in root ,some in folder named admin and some in folder named user aspx pages which are in user...
1
by: Wayne Sepega | last post by:
In our web application we have a routine that will take a URL and encrypt the query string portion of it. We are doing this in the Global.asax in Application_EndRequest, we have done this in 1.1...
0
by: Rahul | last post by:
I am using Application_BeginRequest function in my Global.ascx file and I have a breakpoint set over there. I have a master page and and one of the content of the master page pints to...
3
by: Mike Owen | last post by:
Hi, I have set up a new web site application in VS 2005, and have created a couple of pages which seem to run ok. I then manually added a file called Global.asax.vb, within a folder that I...
3
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi guys I've used an Application_BeginRequest function in my global.asax page to implement some URL rewriting functionality on our website. However, upon moving it to my host (1&1.co.uk), it...
7
by: Joe | last post by:
I'm trying to use the Application_BeginRequest to re-write the path but it doesn't work on the published site when a non-existing URL is called. This does work fine in my dev environment. For...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.