You refer to 'buggy implementations of onload'.
Could this, in practice, result in one or more web controls
appearing as nulls (and crashing the script) when the onload
function fires, even if the controls have been 'wired' into the
Javascript in a conventional fashion? (i.e.
document.getElementById("<%= {control-name}.ClientID %>");
I don't think so. Is that line of JavaScript before or after the
control in question? The control needs to appear before the
JavaScript that accesses it so the browser can put the control in the
page's document object model (DOM) tree.
For example, this HTML will generate an "object required" error
message, because the <INPUT> control comes after the JavaScript
that's trying to access that control's value:
<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT>
var value = document.getElementById("myInputBox").value;
alert(value);
</SCRIPT>
<INPUT type="text" id="myInputBox" name="myInputBox"
value="Hello, world!"/>
</BODY>
</HTML>
But placing the <INPUT> control before the JavaScript will work:
<HTML>
<HEAD>
</HEAD>
<BODY>
<INPUT type="text" id="myInputBox" name="myInputBox"
value="Hello, world!"/>
<SCRIPT>
var value = document.getElementById("myInputBox").value;
alert(value);
</SCRIPT>
</BODY>
</HTML>
This is where the RegisterStartupScript method comes in handy. It
always places the JavaScript at the very end of the HTML, just before
the closing </FORM> tag.
If that's not the problem, what might be happening is either "name
mangling" of the ClientID by ASP.Net, or a known bug in .Net 1.1:
http://support.microsoft.com/default.aspx?id=818803
Concerning name mangling, when multiple instances of the same user
control are present on a page, they cannot all have the same ID in
the generated HTML page. Therefore, ASP.Net gives each instance of
the control a unique name when it generates the HTML.
A control named BlogSideBar1, with an embedded textbox named
SearchBox, would be referenced by C#/VB in an .ascx file as
BlogSideBar1.SearchBox. However, in the HTML it would appear as
_ctl0____ctl0___BlogSideBar1___SearchBox.
Your line of code should work, depending on when it's executed in the
page's life cycle on the server. If there are multiple instances of
"control-name" in your page, that situation might be causing some
kind of problem.
I generally don't put inline <% %> tags in my pages, except for
mundane things like the page's title or meta tags. I prefer to build
the JavaScript, complete with ClientIDs, in my code behind files.
That way I feel I have better control over exactly what gets sent to
the browser, and where it's placed in the page.