Accessing page controls from HTTPModule when custom HTTPHandler defined

Last post 06-24-2009 8:17 PM by Spider Master. 2 replies.

Sort Posts:

  • Accessing page controls from HTTPModule when custom HTTPHandler defined

    06-23-2009, 7:20 PM
    • Member
      point Member
    • Jym
    • Member since 06-23-2009, 11:01 PM
    • Posts 2

    I have a ASP.NET project that uses a custom HTTPHandler for authentication at page level  Additionally I have created a HTTPModule that I want to use to scan controls on a System.Web.UI.Page to make sure they are read-only unless specified in a DB.

     Normally I can get access to the Page object in an HTTPModule by using the HTTPContext.Current.Handler  (see example below) because the Page object is the handler, but because I have created a custom handler the HTTPContext.Current.Handler is no longer the page but my handler instead.

    How can I get access to the Page object when I have a custom HTTPHandler defined? 
    1    public void Init(HttpApplication httApp)
    2    {    
    3        httApp.PreRequestHandlerExecute += new EventHandler(SetControlSecurity);
    4    }
    5    
    6    private void SetControlSecurity(object sender, EventArgs e)
    7    {
    8        HttpApplication app = (HttpApplication)sender;            
    9    
    10       try
    11       {
    12   	System.Web.UI.Page currentPage = (System.Web.UI.Page)HttpContext.Current.Handler;
    13       }
    14       catch (Exception ex)
    15       {
    16   
    17       }            
    18   }
    
     
  • Re: Accessing page controls from HTTPModule when custom HTTPHandler defined

    06-24-2009, 4:40 PM
    • Member
      point Member
    • Jym
    • Member since 06-23-2009, 11:01 PM
    • Posts 2
    After considerable research I have come up with the answer....

    It is not feasible to modify or write to the controls of the Page object from a module.  It is possible to read only.  Here is why.

          The Page object will not exist until a handler has been mapped to the HTTPApplication during the MapRequestHandler event.  Once it is created it is still not in much of a usable form because it has not been rendered yet, basically an empty shell.  Once the Request handler executes then the page has been rendered.  After the handler does its job and the PostRequestHandlerExecute event is called it is now too late to modify the page since it has been rendered.

          The answer is that you need to access the page object in a custom handler instead.   From there you can intercept the page level events and make your modifications.

     

  • Re: Accessing page controls from HTTPModule when custom HTTPHandler defined

    06-24-2009, 8:17 PM
    Answer
    • Participant
      1,580 point Participant
    • Spider Master
    • Member since 10-16-2007, 1:32 PM
    • New Zealand
    • Posts 452

    Just a thought. Have you considered creating your own PageBase Class and inheriting that rather than System.Web.UI.Page

    If you were to do this then you would not need to scan the page becuase you will allready have you answers and full manipulation over rendered controls!!

    Trading Center is a New Classifieds Starter Kit on Code Plex.

    "If Your Question Has Been Answered, Please Mark It As the Answer"

Page 1 of 1 (3 items)