Pindub_Amateur Pindub_Amateur - 1 month ago 11
ASP.NET (C#) Question

item.Selected is false when the checkboxlist is disabled.

I have a checkboxlist where certain items are disabled for certain users.

enter image description here

When I click on 'Save', the below code is executed.

foreach (ListItem item in myCheckBoxList.Items)
{
if (!item.Selected)
{
continue;
}
selectedValues.Add(item.Value);
}


However, item.Selected evaluates to false for the disabled items even though they're selected.

Is there a way to get around this?

Answer

Disabled inputs are never posted to the server, hence it will be set to default value, i.e. false. You can use HiddenField and associate that with each checkbox and set it's value based on it's selection.