Solved

Accessing page property from httpmodule

Posted on 2009-04-01
3
378 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
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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