469,636 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

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
  6.  
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:

[HTML]
<ItemTemplate>
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
</ItemTemplate>
[/HTML]

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 2437

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 this site, you agree to our Privacy Policy and Terms of Use.