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.