in

ASP.NET Weblogs

Andy Smith's Blog

Page.RegisterStartupScript('Andy', 'MetaBuilders_WebControls_GainKnowledge();');

The DataGrid breaks a whole lot of clientscript logic

I just wanted to bring everybody's attention to an insightful newsgroup post by Peter Blum where he exposes a design flaw in the datagrid that affects all control builders out there that emit clientscript.

The summary:
If your control is in a template column of a datagrid, and that column has its Visible property set to false, then your OnPreRender method will still get called. Normally, if Visible is false, because of direct property set or parent visibility, then OnPreRender does not happen. Since most people use OnPreRender to emit/register clientscript, this can cause script to be emitted even tho the control will not be rendered.

This is an especially insidious problem, because your control works just fine in normal testing scenarios. Who would have thought to check for putting the control in an invisible column in a datagrid.

The workaround to avoid script errors in browsers, of course, is to always check for null values from calls to document.getElementById, which would indicate that your control wasn't really rendered to the browser even tho the script was. However, as Peter pointed out, it's a complete waste of bandwidth to emit potentially large scripts when all the controls that the might need the script were never actually rendered.

I guess I'll have to do a code review soon to make sure I'm doing the right thing for this scenario.

Comments

 

Andrew said:

I've had other problems with template columns that are Visible="false".
I didn't have time to investigate so I just added a panel around the content of the template and set it's visibility too.

I know it is just side-stepping the issue, and still sending unneeded content to the browser, but shouldn't the code emited using register client script be code to support your type of control and not a specific instance?

From this post, VGA might disagree, but it would explain a part of why some work in render.
http://weblogs.asp.net/vga/posts/6457.aspx

Proposing that Peter submit this as a wishlist is pretty low.
It is either a bug or design decision we don't understand.
It is not wishfull thinking.
November 5, 2003 5:04 PM
 

Andy Smith said:

The script block as a whole is generally for the control type, yes, but the code gets executed in combination with a RegisterArrayDeclaration for each control instance. As for the link to victor's post... that is kind of unrelated to this issue. We are calling our methods at the proper time. The datagrid is simply not applying the visible property until Render, while the control framework expects it to be finalized for PreRender.
November 5, 2003 5:33 PM
 

Peter Blum said:

Here's an update on this issue. I started a dialog with Microsoft Support a few weeks back. The developers have reviewed the problem and "put in a bugfix request for the next version of the framework". So this is real progress. The techie is also looking into a possible KB article, which was my goal when I contacted them.

Finally, the problem is also in DataGrid headers and footers. If your control is in either of these and they are invisible via ShowFooter=false or ShowHeader=false, your control will get its OnPreRender method called but not Render.
November 23, 2003 9:54 AM

Leave a Comment

(required)  
(optional)
(required)  
Add