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 reason my JavaScript wasn't working: because I had set the controls to be invisible, they weren't being rendered to the browser. ASP.NET will not render an invisible control (rather than rendering it in a non-visible style). However, the control will still receive a ClientID.


Popular posts from this blog

A Generic Method Using HttpClient to Make a Synchronous Get Request

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

How To Mock Out Child Components In Unit Tests of Angular 2 Code