473,480 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

1 New Member
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 2723

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

Similar topics

4
4082
by: mitch-co2 | last post by:
What I am trying to do is when someone clicks on the YES radio button I want the text field called MYTEXT to equal the text field named DATE. The below code works as long as I do NOT UN-COMMENT...
2
3454
by: NishSF | last post by:
Would anyone have any suggestions/javascript code so that if one clicks the Radio Button "Yes" below he has the option of selecting any of the six CheckBox below. If the user clicks on Radio Button...
2
3465
by: Advo | last post by:
Basically, ive got information in a table for the layout purposes, as its text for a questionnaire What i Need, is for instance when the user click a radio button, that information can be...
3
5808
by: Harry Haller | last post by:
I have a radio button group. When the page loads none of them are selected. OnClick selects ONE and clicking another one selects a different one - this is normal behavior. I want to modify it so...
6
5701
by: Rob | last post by:
Hi all, I would like to have a textarea control on my web page that is complete with terms and conditions, in addition, I would like a radio button beneath this to indicate the acceptance of...
3
2486
w33nie
by: w33nie | last post by:
I want to disable the text boxes, captain_name and captain_email, but only if the radio button, captain_guarantee, has NOT been checked. how do i do this? <form name="formTeamApplication"...
6
4795
by: Ang | last post by:
Is it possible to get the checkbox and radio button values from excel by c#? Thanks. *** Sent via Developersdex http://www.developersdex.com ***
2
2795
by: runway27 | last post by:
i am using a self submitting form <form action="<?php echo $_SERVER; ?>" method="POST" id="test2" name="test1"> i need to do a validation of textfields, checkboxes, radio buttons i am able...
3
2764
by: camdev | last post by:
I have a form with 2 radio buttons and multiple checkboxes (see example below). The one radio button indicates all and the other radio button indicates the user has chosen specific options...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7097
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6750
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.