我使用CheckBoxList
来显示一组业务单位。我想将选择限制为最多5项,如果用户选择第6项,那么我应该显示警告并取消选择第6项。
控制如下:
<asp:CheckBoxList ID="ckBLBusinessUnits" onclick="loader(this.id);" runat="server" AutoPostBack="True" Visible="false" OnSelectedIndexChanged="ckBLBusinessUnits_SelectedIndexChanged"></asp:CheckBoxList>
我正在使用的JS函数是:
function loader(controlID) {
modal = document.getElementById('loadingImage');
modal.style.display = "block";
if (controlID == "ckBLBusinessUnits")
{
if (($('#ckBLBusinessUnits :checkbox:checked').length) > 5)
{
alert("Max 5 BU's can be selected");
//is it possible to uncheck here? I am unable to find any method to do so.
}
}
}
C#如下:
protected void ckBLBusinessUnits_SelectedIndexChanged(object sender, EventArgs e)
{
if (selectedValues.Count <= 5)
{
//Do something and then disable the loader.
loadingImage.Style.Add("display", "none");
}
else if (selectedValues.Count > 5)
{
//is it possible to uncheck here? I am unable to find any method to do so.
}
}
答案 0 :(得分:0)
这可能不是您要求的,但它可能适用于您的情况。每当所选项目总数为5时,禁用其他复选框,而不是取消选中该复选框,并显示您的提醒。
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
List<ListItem> SelectedItems = new List<ListItem>();
foreach (ListItem ItemsSelected in CheckBoxList1.Items)
{
if (ItemsSelected.Selected)
SelectedItems.Add(ItemsSelected);
}
if(SelectedItems.Count() == 5)
{
// Display alert
foreach(ListItem item in CheckBoxList1.Items)
{
if(!SelectedItems.Contains(item))
{
item.Enabled = false;
}
}
} else
{
foreach (ListItem item in CheckBoxList1.Items)
{
item.Enabled = true;
}
}
}