Last post Dec 23, 2009 05:11 PM by squebler
Sep 12, 2007 08:53 AM|degt|LINK
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.
Sep 12, 2007 09:40 AM|jshepler|LINK
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?
Sep 12, 2007 09:49 AM|jackyang|LINK
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.
Sep 12, 2007 12:23 PM|degt|LINK
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.
Dec 23, 2009 05:11 PM|squebler|LINK
Another reason that OnPreRender or other event handler methods might not fire is because the page is on a callback instead of a postback.