Solved

Accessing page property from httpmodule

Posted on 2009-04-01
3
380 Views
Last Modified: 2012-05-06
I have a page (TestPage.aspx) - in the codebehind just after the partial class definition, I've created a public property get/set called Hello.  ex:

public partial class TestPage: System.Web.UI.Page {
  string _hello = "test";
  public string Hello { get { return _hello ;} set { _hello = value; } }
  protected void Page_Load(object sender, EventArgs e) { }
}

I have a HTTPModule that I have hooked an event in to Application_PreRequestHandlerExecute.

The source/sender/whatever for this particular event is the Application object, and I am interested in playing if the Handler is of Page type.  So I have:

private void Application_PreRequestHandlerExecute(object source, EventArgs e) {
    HttpApplication application = (HttpApplication)source;
    IHttpHandler handler = application.Context.Handler;
    if (handler is Page) {
       string test = ((Page)handler).Hello;   //this bombs out
   }
}

By stepping through the code, I see that my public property Hello on the Page is read and populated before the HTTPModule event is invoked.  However, I cannot seem to get at this public property.  ((Page)handler).Hello fails.

If I break on this line and open the Watch window and put in ((Page)handler) I can drill down into the object by expanding the plus signs, and all I see are the regular Page methods/properties - I do not see my public property listed among them.

HOWEVER... the very first entry in the Watch below ((Page)handler) is [ASP.TestPage.aspx], if I expand that, there's base and static members.  If I expand base, I see all of the controls and properties for that page.. including my public property Hello.

I assume that the Watch window is populating this stuff as I drill down, so maybe this stuff isn't available at this point of the lifecycle?

Anyone have a crafty way to get at this property, or is it simply not available until later in the lifecycle?
0
Comment
Question by:wegee2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Accepted Solution

by:
wegee2 earned 0 total points
ID: 24040102
My interim solution involves using Reflection and might not be the best way to approach this.  I'm really looking forward to one of you guru-types telling me if there's a better way.
PropertyInfo checkHello = handler.GetType().BaseType.GetProperty("Hello");
string test;
if (checkHello != null) {
   test = checkHello.GetValue(handler, null).ToString();
}

Open in new window

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 24047489
It's failing because Hello isn't declared in Page it is declared in TestPage. So if you cast to Page then you cannot access properties defined in a class that derives from it.
0
 

Author Comment

by:wegee2
ID: 24086617
That's why I ended up in Reflection land to dig down and get the classname of the page.

Is there some way to cast the handler as (TestPage) without knowing in advance that it's (TestPage)?  This is in an HttpModule that handles the entire application, not just one page, so I can't test for both.

A custom base Page type is another option I suppose, but I would rather not go that route.

I might be stuck with my Reflection method.  Judging from the lack of response I am presuming that there's no other options.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question