Solved

Accessing page property from httpmodule

Posted on 2009-04-01
3
373 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
  • 2
3 Comments
 

Accepted Solution

by:
wegee2 earned 0 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now