asp.net building controls:
When a WebControl is set to Visible = false, the OnPreRender and Render
methods are both skipped. (Control.PreRenderRecursiveInternal skips
OnPreRender on children; Control.RenderControl skips Render on children.)
DataGrid always calls the OnPreRender event, even when the control is
contained in a Column whose Visible property is false or footer template
whose ShowFooter property is false.
Since OnPreRender is often used to register client-side scripts, and those
scripts often depend on the HTML created in the Render method, it makes
sense to block OnPreRender when the control will not be rendered. As a
result of the DataGrid's design, any custom control that generates such
client-side code may be break in the DataGrid when it would work fine
elsewhere. (In other words, expect JavaScript errors.) I could not find
anywhere in the .NET documentation stating that the Visible property will
cause the OnPreRender method to be skipped when false. However, the
documenation says the control will not be rendered when Visible = false.
Since OnPreRender is documented as preparing for the Render phase, I feel
this is really a bug.
The worst part of this that I cannot find any way for a web control to
detect that it will not be rendered. Normally, the Visible property on a
control is false when any parent's Visible property is false. In the
DataGrid, its still Visible=true even though the DataGridColumn containing
it is Visible=false. I'd like to find a low impact way of detecting this
control will not be rendered so it doesn't output its client-side script.
(Yes, I will make my client-side scripting more defensive but I'd like to
push for the server-side code working consistently.)
I'd like Microsoft's view on this and if nothing else, to get this issue
clearly documented.
--- Peter Blum
www.PeterBlum.com