Disable CheckboxList Items

I have a checkboxlist with several items checked by default.  Is there any way to prevent a user from unchecking these items?
LVL 1
abenageAsked:
Who is Participating?
 
FocusynCommented:
The Checkboxlist is a table or array collection, and is not designed with the capability of setting different behavioral properties for individual items; In fact, it was designed for exactly the opposite reason: to allow the programmer to use a single control when all behaviors are identical for a ListItem collection.  To dynamically set behavior by item, you need to use the regular checkbox control instead of the CheckBoxList.  With the list, they will either all be enabled or disabled, and as far as I know it's impossible to change that without seriously manipulating the control via manual overloads etc.  

For the sake of time savings, here's what I would recommend:

You're using a data table or array to populate the items, and then using some dynamic property to decide which should be selectable or not selectable by the user.  The best practice would be to run through that table in a Loop manually adding an individual checkbox control with visible=true, checked=True/False depending, but enabled=true/false depending on whatever condition is deciding whether or not it should be modifiable. Visible=True and Enabled=False on the same control is what gives you that "grayed out" look.

Ok, so all that is a lot of iteration and possibly complex programming depending on your idividual skill level.  So in my classic style,
here's a Jerry-rig you can do if you just need to get this out quickly and can't or don't have time to do iterations and programatically created controls:

Add a panel to your page/form and use two checkboxlist controls.  Parse through your full datasource and at each record, insert into one of two arrays or tables, depending on whether or not the user should be allowed to modify that item.  Bind one checkboxlist to the 'userCANmodify' set and the other to the 'userCANTmodify' set.  Set both checkBoxLists to visible=True, and enabled=True for the one with datasoure 'userCANmodify' and enabled=False to the one bound to 'userCANTmodify' datasource.  Really the only downside to that is the sorting, but arguably, it makes it easier on the user because he or she doesn't have to look through a list of unchangable items to find the one he or she needs to change.

Only other workaround I know of for this is to either set the whole control to autopostback and keep re-checking the item when the user unchecks it, or explicitly resetting the collection to checked=true on form post.  Either of those are a bad idea because the user doesn't know unless you also add code to notify the user of such, and if you're going to spend that much time and coding, you may as well just do it properly with individual checkbox controls anyway.
0
 
g_johnsonCommented:
I don't have .Net with me right now, but I think you can use the ItemCheck event and just set it back to checked if they try to uncheck it.
0
 
abenageAuthor Commented:
OK.  Thanks for the insight.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.