Solved

Accessing page property from httpmodule

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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