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

Home Posts Topics Members FAQ

GridView checkbox as ReadOnly

58 New Member
Hi folks,

I'm using ASP.NET with C# code behind. I've got a GridView on my page that has several checkbox fields that are bound to database fields. I want the checkboxes to be visible and editable during normal "EditItemTemplate". However during "ItemTemplate" display, I want it to be ReadOnly. In other words, unless the users are in edit mode, they cannot toggle the checkbox. Apparently checkboxes don't have a ReadOnly option. I've tried setting "Enabled" to false and it works, although it's too grayed-out for my tasted and I'd rather just set it to read only.

Any idea how to do this?

Robert
May 5 '08 #1
6 9940
RobertTheProgrammer
58 New Member
I've got the workaround working... I use the OnCheckedChanged event to call code behind that resets the checkbox to it's original value. It works, but it's cumbersome because of all the overhead of reloading the page.

If anyone has any better ideas...

Robert
May 7 '08 #2
Frinavale
9,735 Recognized Expert Moderator Expert
I've got the workaround working... I use the OnCheckedChanged event to call code behind that resets the checkbox to it's original value. It works, but it's cumbersome because of all the overhead of reloading the page.

If anyone has any better ideas...

Robert
You could set the checkbox's Enabled value to False...
or you could use JavaScript to do this....

Again you would do this in your RowDataBound event for your GridView...
Expand|Select|Wrap|Line Numbers
  1. Private Sub MyGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GV_MyGridView.RowDataBound
  2.         Dim cb As CheckBox
  3.         If e.Row.RowType = DataControlRowType.DataRow Then
  4.             cb = CType(e.Row.FindControl("MyCheckboxName"), CheckBox)
  5.             If Not cb Is Nothing Then
  6.                 cb.Enabled = False
  7.                 'cb.Attributes.Add("onclick","javascript:document.getElementById('" + cbl.ClientID + "').checked=false;")
  8.             End If
  9.         End If
  10.     End Sub

Cheers!

-Frinny
May 7 '08 #3
RobertTheProgrammer
58 New Member
I've tried the Enabled=false attribute and it works well, but the checkbox is too grayed out. My background is gray and it makes the checkbox almost vanish. I'd much rather have a ReadOnly type of option so that clicking on the checkbox would simply do nothing, but it would look relatively normal.

Robert
May 7 '08 #4
Frinavale
9,735 Recognized Expert Moderator Expert
I've tried the Enabled=false attribute and it works well, but the checkbox is too grayed out. My background is gray and it makes the checkbox almost vanish. I'd much rather have a ReadOnly type of option so that clicking on the checkbox would simply do nothing, but it would look relatively normal.

Robert
There is no ReadOnly property on checkboxes...I'd suggest using the JavaScript solution then.

-Frinny
May 7 '08 #5
DrBunchman
979 Recognized Expert Contributor
Another option would be to use an image that looked like a checkbox which you could display when you wanted it to be 'Read Only' rather than the checkbox itself.

Dr B
May 8 '08 #6
RobertTheProgrammer
58 New Member
Ah! The checkbox image is brilliant! I'll do that. Thanks!

Robert
May 8 '08 #7

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

Similar topics

6
by: Dabbler | last post by:
I have a GridView with a couple of TemplateFields. When I click the update link the Gridview returns to display mode with no errors but the data from bound text fields or dropdown list isn't...
9
by: hazz | last post by:
I want to display 'n' records for a table-driven data entry page. The first column should be readonly and the 2nd column, a checkbox WRITABLE (NOT READONLY). I can't use the gridview because it...
0
by: hazz | last post by:
The following code generates checkbox values from the database. Once the user selects/deselects checkbox values, is there a way to obtain the ID and Checkbox.Checked state from these BoundColumns...
2
by: Chris Davoli | last post by:
How do you enable a check box in the GridView. I selected Checkbox Field in the Columns of the GridView, and the check box shows up in the Grid view, but it is disabled. How do I enable it so I can...
3
by: r.hein | last post by:
I've got a middle tier data object that I'm binding a gridview to, and I'm having some problems with the check box, and having it enabled. Let'd start with the middle tier object (and the...
0
by: landesjoe | last post by:
Hi, here's my problem in short: Text boxes in gridview don't seem to hold their value if the column's .Visible property is changed back and forth. I've got a form with a gridview populated from...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
15
by: stefbek97 | last post by:
Hello All, I am having Trouble with a Gridview control In my asp.net/C# application. It contains Some boundFields and one checkbox field. My gridview allows Editing. It succesfully enters edit...
1
BeemerBiker
by: BeemerBiker | last post by:
I converted an arraylist to a dataset and bound the dataset to a gridview but I cannot change the value of the checkbox in the gridview. I identified the DataKeyNames and made sure that ReadOnly was...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.