Question : Disabling Only Checkboxes in Checkboxlist Control

I have a web form comprised of a databound checkboxlist that has some html formatting in the labels of checked items.  The control is enabled for editing for certain roles but is generally disabled for the public.  Everything is fine except in IE where disabling grays out all items and the label formatting is gone making it difficult to read.  Is there a way to programatically disable only the checkbox in this control while leaving the label alone?

Answer : Disabling Only Checkboxes in Checkboxlist Control

The code above was for a CheckBox, not for a CheckBoxList. One might expect a CheckBoxList to contain as many checkboxes as there are listitems, but unfortunately that isn't true. Instead, generally, there's only one CheckBox, which is the first item in the Controls array. Because we cannot know for certain that the inner workings of CheckBoxList will always remain the same, we take a few precautions: check the type and loop through all controls (even though we know it is only one control currently):
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
// same PreRender as above, but now for a CheckBoxList
protected void cblHalfEnabled_PreRender(object sender, EventArgs e)
{
    CheckBoxList cbl = (CheckBoxList)sender;

    // loop through all Controls that are of type CheckBox: that's actually only one.
    foreach (Control checkbox in cbl.Controls)
    {
        if (checkbox is CheckBox)
            (checkbox as CheckBox).InputAttributes.Add("disabled", "disabled");
    }
}
Random Solutions  
 
programming4us programming4us