ASP.NET 2.0 does great things but like its ancestors also has holes: things we need to do but can't -- or can't without dodging around it with inside knowledge, JScript and other tricks. AspNet2Holes is about those holes and the ways to step around them.

Tuesday, July 04, 2006

DIV and conquer

ASP.NET 2.0 becomes most unfriendly to JavaScript when you want to change the visibility of Web form controls. Documentation won't explain how to do this, but it's simple. Set style.visibility to "visible" or "hidden" for most browsers. Problems arise when you want to change controls such as CheckBox or RadioButton that generate multiple elements, an input and a label in these cases. Addressing a control in the usual way affects only one element. In these cases, it does not change the label.

The solution is to put an HTML DIV element around one or more controls that you want to change, such as:

Be sure to put "id" in lower case, or Visual Studio 2005 will bark at you with a diagnostic such as "Error...name contains uppercase characters, which is not allowed," a rule routinely violated by its own markups.

With such a div element in place, show or hide everything that it brackets with JavaScript statements such as:

About Me

Craig BolonCommercial software development over 25 years in Windows and Linux, before that in DOS, Unix, VMS and several others, more than 20 languages. Working now mostly in ASP.NET with C#, SQL and JavaScript.
View my complete profile