locked
OnPreRender method of WebControl is not being called RRS feed

  • Question

  • User-881099725 posted

    I took some of my old code and converted it to a custom web control which was more appropriate for what it did. In my project I created a custom webcontrol I named ContentMapperControl and it derives from the System.Web.UI.WebControls.WebControl class.

     
    This control either renders content or not. If it cannot render I let the containing page enable some other controls instead. So for that reason in my custom web control I overrode OnPreRender(). In that override I perform the necessary preprocessing. At the end of OnPreRender a property named "CanRender" is set to true if it can deliver content. Then in the RenderContents method of the WebControl it does the actual rendering of content.

     Now, On my containing page's Page_Load I was checking the web control's CanRender property but it is always false even though the requested content is available. In my debugging I placed a breakpoint on the OnPreRender method of the custom web control but it never comes there.

    So, what is wrong here? why is OnPreRender not being called for this control? I just noticed that if I set the control by default to Visible then OnPreRender is called, but if I set it to not visible so that I can make it visible conditionally then OnPreRender is not called.
     

    Wednesday, September 12, 2007 8:53 AM

Answers

  • User-590788913 posted

    If the server control's Visible is false prior PreRender, it will not fire the event. Just make sure the Visible property is true prior PreRender, and code the logic not to show certain controls. Or you can defer the rendering in Render event, and do the logic of setting CanRender in PreRender.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2007 9:49 AM

All replies

  • User2019766093 posted

    I do know that the page will not call a control's OnRender() method if the control's Visible property is false. Perhaps this applies to PreRender as well?

    The page's Page_Load() is called before the controls' OnPreRender() method is called. Since you're setting the CanRender property at the end of the control's OnPreRender() method, the page's Page_Load() is the wrong place to check that value.

    Since the page will use the control's Visible property to determine whether to render the control or not, can you not just set the Visible property and let the page do its thing?

    /js

    Wednesday, September 12, 2007 9:40 AM
  • User-590788913 posted

    If the server control's Visible is false prior PreRender, it will not fire the event. Just make sure the Visible property is true prior PreRender, and code the logic not to show certain controls. Or you can defer the rendering in Render event, and do the logic of setting CanRender in PreRender.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 12, 2007 9:49 AM
  • User-881099725 posted

    Having realised of the workings of the Visible property on OnPreRender I changed the logic of my control. The page still checks on Page_Load for CanRender but the web control now does its setup stuff in the OnInit method where it gets the information it needs and sets/clears the CanRender property.

    Wednesday, September 12, 2007 12:23 PM
  • User1030552478 posted

    Another reason that OnPreRender or other event handler methods might not fire is because the page is on a callback instead of a postback.

    Wednesday, December 23, 2009 5:11 PM