I have the following checkboxlist. I need to uncheck rest of list items when "None" is selected. How do I do that? Thanks
<asp:CheckBoxList ID="CheckBoxList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Value="1">French</asp:ListItem>
<asp:ListItem Value="2">Spanish</asp:ListItem>
<asp:ListItem Value="3">Italian</asp:ListItem>
<asp:ListItem Value="4">German</asp:ListItem>
<asp:ListItem Value="5">Portuguese</asp:ListItem>
<asp:ListItem Value="6">Chinese</asp:ListItem>
<asp:ListItem Value="7">Japanese</asp:ListItem>
<asp:ListItem Value="8">Russian</asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
<asp:ListItem Value="None">None</asp:ListItem>
</asp:CheckBoxList>
UPDATE: Translation to vb.net of answer below by Darin Dimitrov
Public Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
Dim listItems = CheckBoxList1.Items.Cast(Of ListItem)()
Dim isNoneSelected = listItems.Any(Function(x) x.Value = "None" AndAlso x.Selected = True)
If isNoneSelected Then
listItems.Where(Function(x) x.Value <> "None").ToList().ForEach(Function(x) InlineAssignHelper(x.Selected, False))
End If
End Sub
There are two possibilities:
Server side: subscribe for the OnSelectedIndexChanged
event and enable AutoPostBack:
<asp:CheckBoxList ID="CheckBoxList1" RepeatDirection="Horizontal" runat="server" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"> <asp:ListItem Value="1">French</asp:ListItem> <asp:ListItem Value="2">Spanish</asp:ListItem> <asp:ListItem Value="3">Italian</asp:ListItem> <asp:ListItem Value="4">German</asp:ListItem> <asp:ListItem Value="5">Portuguese</asp:ListItem> <asp:ListItem Value="6">Chinese</asp:ListItem> <asp:ListItem Value="7">Japanese</asp:ListItem> <asp:ListItem Value="8">Russian</asp:ListItem> <asp:ListItem Value="Other">Other</asp:ListItem> <asp:ListItem Value="None">None</asp:ListItem> </asp:CheckBoxList>
and in the code behind:
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) { var listItems = CheckBoxList1.Items.Cast<ListItem>(); var isNoneSelected = listItems.Any(x => x.Value == "None" && x.Selected == true); if (isNoneSelected) { listItems.Where(x => x.Value.= "None").ToList().ForEach(x => x;Selected = false); } }
Client side: use javascript to detect when the user clicks on the None checkbox and uncheck the others.
<asp:CheckBoxList ID="CheckBoxList1" RepeatDirection="Horizontal" runat="server" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
<asp:ListItem Value="1">French</asp:ListItem>
<asp:ListItem Value="2">Spanish</asp:ListItem>
<asp:ListItem Value="3">Italian</asp:ListItem>
<asp:ListItem Value="4">German</asp:ListItem>
<asp:ListItem Value="5">Portuguese</asp:ListItem>
<asp:ListItem Value="6">Chinese</asp:ListItem>
<asp:ListItem Value="7">Japanese</asp:ListItem>
<asp:ListItem Value="8">Russian</asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:CheckBoxList>
<table>
<tr>
<td>
<asp:CheckBox ID="chkNone" Text="None" runat="server" AutoPostBack="True" OnCheckedChanged="chkNone_CheckedChanged" />
</td>
</tr>
</table>
Removed the "None" option from the list so that we can unselect "None" if the user decide to make a language selection.
/// <summary>
/// clear the list
/// and checked "none" as SelectedIndexChanged resets it
/// </summary>
protected void chkNone_CheckedChanged(object sender, EventArgs e)
{
CheckBoxList1.ClearSelection();
chkNone.Checked = true;
}
/// <summary>
/// If a user makes a selection clear the "none" checkbox
/// </summary>
protected void cklWatchMoveVideosIf_SelectedIndexChanged(object sender, EventArgs e)
{
chkNone.Checked = false;
}
The checkboxlist and the checkboxOther could be added to an updatePanel to prevent a full postback, which makes this a bit smoother.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.