We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Disable CheckboxList Items

abenage
abenage asked
on
Medium Priority
948 Views
Last Modified: 2008-01-09
I have a checkboxlist with several items checked by default.  Is there any way to prevent a user from unchecking these items?
Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
OK.  Thanks for the insight.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.