Showing posts from May, 2008

ASP.NET ClientID Weirdness

I recently ran across something "funny" in ASP.NET. Not "ha ha" funny, but "hmm" funny.

I had a page wherein I wanted to hide some of my server-side controls, but add an HTML button and a JavaScript function to make them visible when the button was clicked. Sounds simple enough, right? Of course, to access those server-side controls from JavaScript, I'd need their ClientIDs, which I embedded into my client-side script using server-side scripting tags (<% and %>). This worked like a charm -- when viewing the page source from my browser, the server-side tags had indeed been replaced with the appropriate ClientIDs. However, any attempt to access these controls using JavaScript's getElementById() function proved fruitless -- the function returned null for these controls.

I turned on page tracing (using the Trace="true" option in the Page declaration) and verified these controls existed -- however, here's the funny part, and the …