Im new to asp.net... I have a checkboxlist with 6 checkboxes when 2 i selected i wont the other to be disabled, and if 1 is unchecked i want all to be enabled again
This is my code in aspx
<asp:CheckBoxList ID="CheckBoxList1" runat="server" TabStop="false" CellPadding="5" CellSpacing="10" Width="420px" onclick="javascript:CheckCheck();" Font-Size="X-Large">
<asp:ListItem Text="Texas NL 10/10" Value="" TabStop="false" TabIndex="-1"></asp:ListItem>
<asp:ListItem Text="Texas NL 25/25" Value="" TabStop="false" TabIndex="-1"></asp:ListItem>
<asp:ListItem Text="Texas 50/50" Value="" TabStop="false" TabIndex="-1"></asp:ListItem>
<asp:ListItem Text="Omaha PL 10/10" Value="" TabStop="false" TabIndex="-1"></asp:ListItem>
<asp:ListItem Text="Omaha PL 25/25" Value="" TabStop="false" TabIndex="-1"></asp:ListItem>
<asp:ListItem Text="Omaha PL 50/50" Value="" TabStop="false" TabIndex="-1"></asp:ListItem>
</asp:CheckBoxList>
Please help
CodePudding user response:
Hum, what a cute little problem!
You could remove the client side click, set autopostback = true, and then use this code:
Protected Sub CheckBoxList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckBoxList1.SelectedIndexChanged
' count number checked.
Dim CheckCount As Integer
For Each MyCheck As ListItem In CheckBoxList1.Items
If MyCheck.Selected Then
CheckCount = 1
End If
Next
If CheckCount = 2 Then
' disable all boxes except those checked
For Each MyCheck As ListItem In CheckBoxList1.Items
If Not MyCheck.Selected Then
MyCheck.Enabled = False
End If
Next
End If
If CheckCount < 2 Then
' enable all check boxes
For Each MyCheck As ListItem In CheckBoxList1.Items
MyCheck.Enabled = True
Next
End If
End Sub
CodePudding user response:
I have this code today, but it shows an alert message, and i want it to disable the boxes instead
function CheckCheck() {
var chkBoxList = document.getElementById('<%=CheckBoxList1.ClientID %>');
var chkBoxCount = chkBoxList.getElementsByTagName("input");
var btn = document.getElementById('<%=btnSubmit.ClientID %>');
var i = 0;
var tot = 0;
for (i = 0; i < chkBoxCount.length; i ) {
if (chkBoxCount[i].checked) {
tot = tot 1;
}
}
if (tot > 2) {
alert('You can only pick 2 games'); btn.disabled = true;
}
else {
btn.disabled = false;
}
}