[英]uncheck all items of checkboxlist select in c#
I have this check box in my ascx file :我的 ascx 文件中有这个复选框:
<asp:CheckBoxList ID="chk" runat="server" RepeatLayout="Table" RepeatDirection="Horizontal" RepeatColumns="1">
i want to have a option to select no options in list.我想有一个选项来选择列表中的任何选项。 but my checkboxlist choose first option by default.and when i uncheck it and save and load page first option is seleted.
但我的复选框列表默认选择第一个选项。当我取消选中它并保存和加载页面时,第一个选项被选中。
I use this code to unselect but not working:我使用此代码取消选择但不起作用:
if (string.IsNullOrEmpty(chk.SelectedValue))
{
foreach (ListItem item in chk.Items)
{
item.Selected = false;
}
chk.SelectedIndex = -1;
}
I use this code in Page_Load and Page_PreRender of my page.我在页面的 Page_Load 和 Page_PreRender 中使用此代码。 and chk.SelectedValue is null or empty.
并且 chk.SelectedValue 为 null 或为空。 but first items is cheked
但第一项被检查
I use this code for bind data to checkboxlist:我使用此代码将数据绑定到复选框列表:
var it = new ListItem {Text = arr1[0], Value = arr1[1];
chk.Items.Add(it);
Your code is pointless.你的代码毫无意义。 You are checking if no item is selected(
string.IsNullOrEmpty(chk.SelectedValue)
), then you want to unselect all.您正在检查是否未选择任何项目(
string.IsNullOrEmpty(chk.SelectedValue)
),然后您想取消选择所有项目。
I assume you want the opposite:我假设你想要相反的:
if (chk.SelectedIndex != -1) // you can also use chk.SelectedValue != "", default is an empty string ("")
{
//foreach (ListItem item in chk.Items)
//{
// item.Selected = false;
//}
chk.SelectedIndex = -1;
}
However, the loop is redundant since SelectedIndex = -1;
然而,循环是多余的,因为
SelectedIndex = -1;
does the same in one statement.在一个语句中做同样的事情。
Call:致电:
CheckBoxList1.ClearSelection();
on pageload.在页面加载上。
for (int i = 0; i< chk.Items.Count; i++)
{
chk.SetItemChecked(i, false);
}
//chk.SelectedIndex = -1;
This helped me solve my problem to uncheck all checked items of checkboxlist C# (which is also in the title of this post): uncheck all items of checkboxlist select in c # Thank you Lucas Baum!这帮助我解决了取消选中复选框列表 C# 的所有已选中项目的问题(这也在本文的标题中):取消选中 C# 中的复选框列表选择的所有项目,谢谢 Lucas Baum!
for (int i = 0; i < chk.Items.Count; i++)
{
chk.SetItemChecked(i, false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.