Q:

Limit Number of Checked CheckBoxes in Dynamically Populated CheckBoxList

I have a CheckBoxList that I populate from SQL Server:
<asp:CheckBoxList runat="server" ID="cblItems" DataSourceID="sqlItems"
     DataTextField="Item" DataValueField="ID" RepeatLayout="Table"
     RepeatDirection="Horizontal" RepeatColumns="2" />

This is populated by a SqlDataSource sqlItems (as you can see in the DataSourceID field).
What I would like to do is only allow a maximum of two checkboxes to be checked at a time. I originally attempted this with a SelectedIndexChanged handler even though I figured it likely wouldn't work.
Protected Sub cblItems(sender As Object, e As EventArgs) Handles cblItems.SelectedIndexChanged
     dim numSelected as Integer = 0

     For Each li as ListItem in cblItems.Items
          If li.selected then
                numSelected = numSelected + 1
          End If
     Next

     If numSelected = 2 then
          MsgBox("Disable CheckBoxes Here")
     End If
End Sub

As you can see, I never got to the point to even disable the non-checked CheckBoxes, as the SelectedIndexChanged never fired. How can I achieve this?

A:

You need to set OnSelectedIndexChanged and AutoPostBack="True":
<asp:CheckBoxList runat="server" ID="cblItems" DataSourceID="sqlItems"
     DataTextField="Item" DataValueField="ID" RepeatLayout="Table"
     RepeatDirection="Horizontal" RepeatColumns="2" 
     AutoPostBack="True" OnSelectedIndexChanged="cblItems"/>

