Disable CheckboxList Items

Posted on 2006-03-24
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?
Question by:abenage
    LVL 4

    Expert Comment

    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.
    LVL 7

    Accepted Solution

    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.
    LVL 1

    Author Comment

    OK.  Thanks for the insight.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now