ASP.NET, Checkbox, GridView, HTML, JavaScript, Programming, WebForms

Referencing GridView Controls Using JavaScript

Today Luvy and I looked at updating the logic for an ASP.NET forms page that we are working on. The page contains a multi-row update-able GridView, and we were asked to make sure that if one CheckBox control was unchecked, that another CheckBox  control on the same grid line would also uncheck itself.

We chose to handle this logic at the client side via JavaScript since a postback would certainly be overkill for handling such a straightforward rule.

When adding JavaScript to an ASP.NET control there are two ways to do so. A JavaScript onClick event cannot be added directly to the HTML markup for the control, so the options are to add the JavaScript event either in the .NET control’s Render event, or to instantiate & update the control in the GridView’s Render event.

In our case, we are performing a number of control adjustments in the GridView’s Render event, and I like to keep GridView logic in one place for readability reasons. So we went with the option of instantiating & updating the control in the GridView’s Render event rather than the CheckBox control’s Render event.

To summarize the code, it went something a little like this: Continue reading “Referencing GridView Controls Using JavaScript”