472,119 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Read only checkbox, radio button (in ASP.NET GridView) solution

This is a solution to the problem. Works with .NET 2.0.

So the problem is displaying a data bound read-only checkbox or radio button in a GridView without actually disabling those controls.

I assume you know what templates are in a GridView. Examples are in VB.NET. Sorry...

So as a first step create a function in the page's underlying class that returns the string "checked" based on the bound data. Here's what I did. The underlying data table is storing bool values in bit fields.

Expand|Select|Wrap|Line Numbers
  1. Protected Function TrueToChecked(ByVal str As String) As String
  2.   Dim retval As String = ""
  3.   If "True" = str Then retval = "checked"
  4.   Return retval
  5. End Function
Then create an ItemTemplate that uses regular HTML <input...> type radio button/checkbox controls. You have to embed a call to the above function in the tag. The parameter for the function will be the output of the Eval() function. It goes something like this:

exclude<input type="radio" <%# TrueToChecked(Eval("force_exclude")) %> onclick="window.focus();return false;"/>
<input type="radio" <%# TrueToChecked(Eval("force_include")) %> onclick="window.focus();return false;"/>include

The TrueToChecked() function will be called with the string representation of the underlying data, every time the DataBind method of the ItemTemplate is called.

The control will be readonly thanks to the small JavaScript code hooked up to the 'onclick' event of the control.

Note, that the controls do not have the 'name' attribute defined. If you give a name, the radiobuttons will be treated as one group, and clicking on one, will uncheck all the others even though the JavaScript will prevent updating any of them. If for some reason you need to give names, add another function to the class that returns a unique name on every call. You can call this function to return a string as name the same way the TrueToChecked() function is called in the example. For checkboxes it does not matter.
Mar 29 '07 #1
0 2573

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Harry Haller | last post: by
6 posts views Thread by Ang | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.