you can apply a simple logic to make items
disabled
if selected item count reach to a limit.
I tried and I found a solution. have a look on it
<script language="javascript" type="text/javascript">
function disableAfterSelecton(ctrl) {
var disableItemsAfetrSelection = ctrl.getAttribute('maxSelectedCount', 0);
var inputRefArray = ctrl.getElementsByTagName('input');
if (getSelectedCount(ctrl) == disableItemsAfetrSelection) {
for (i = 0; i < inputRefArray.length; i++) {
if (!inputRefArray[i].checked) makeItemEnableDisable(inputRefArray[i], false);
}
}
else if (getSelectedCount(ctrl) < disableItemsAfetrSelection) {
for (i = 0; i < inputRefArray.length; i++) {
if (!inputRefArray[i].checked) makeItemEnableDisable(inputRefArray[i], true);
}
}
}
function getSelectedCount(ctrl) {
var inputRefArray = ctrl.getElementsByTagName('input');
var checkedCount = 0;
for (i = 0; i < inputRefArray.length; i++) {
if (inputRefArray[i].checked) ++checkedCount;
}
return checkedCount;
}
function makeItemEnableDisable(item, isEnabled) {
if (!isEnabled)
item.disabled = 'disabled';
else
item.disabled = '';
}
</script>
here is the CheckBoxList code
<asp:CheckBoxList ID="chkItems" runat="server" maxSelectedCount="3" onclick="return disableAfterSelecton(this);">
Here
maxSelectedCount
is the custom property which you can decide based on your requirement. This is my idea, you can make the code looks better.
Thanks