Link to home
Start Free TrialLog in
Avatar of wegee2
wegee2

asked on

Accessing page property from httpmodule

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?
ASKER CERTIFIED SOLUTION
Avatar of wegee2
wegee2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Carl Tawn
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.
Avatar of wegee2
wegee2

ASKER

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.