C# 选中/取消选中 ASP.NET 中 CheckBoxList 中的所有项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19010069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:53:14  来源:igfitidea点击:

Check/Uncheck All items in CheckBoxList in ASP.NET

c#asp.net

提问by Samiey Mehdi

I have a CheckBoxList like following

我有一个 CheckBoxList,如下所示

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CheckBoxList1.Items.Add(new ListItem("Check/Uncheck All","0"));
        CheckBoxList1.Items.Add(new ListItem("A","1"));
        CheckBoxList1.Items.Add(new ListItem("B","2"));
        CheckBoxList1.Items.Add(new ListItem("C", "3"));
        CheckBoxList1.Items.Add(new ListItem("D", "4"));
    }        
}

I want whenever the first item is checked to check the rest of the items and whenever unchecked to uncheck the rest. Also the user can select every item separately.

我希望无论何时检查第一个项目以检查其余项目,以及何时取消检查其余项目。用户也可以分别选择每个项目。

I want do this with code behind without JavaScript or JQuery.

我想在没有 JavaScript 或 JQuery 的情况下使用代码来做到这一点。

采纳答案by Samiey Mehdi

Try this

尝试这个

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string result = Request.Form["__EVENTTARGET"];
    int index1 = int.Parse(result.Substring(result.IndexOf("$") + 1));
    if (index1 == 0)
    {
        bool tf = CheckBoxList1.Items[index1].Selected ? true : false;
        CheckUncheckAll(tf);
    }
}
void CheckUncheckAll(bool tf)
{
    foreach (ListItem item in CheckBoxList1.Items)
    {
        item.Selected = tf;
    }
}

回答by Akash Sarawagi

in .aspx page please add autopostback="true" for checkboxlist

在 .aspx 页面中,请为复选框列表添加 autopostback="true"

then please add this event. Its working i have checked it.

那么请添加这个事件。它的工作原理我已经检查过了。

Private Sub CheckBoxList1_SelectedIndexChsanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
    Dim result As String = Request.Form("__EVENTTARGET")
    Dim checkedBox As String() = result.Split("$"c)
    Dim index As Integer = Integer.Parse(checkedBox(checkedBox.Length - 1))
    If CheckBoxList1.Items(index).Text = "Check/Uncheck All" Then
        Dim Chkbool As Boolean = CheckBoxList1.Items(index).Selected
        For Each item In CheckBoxList1.Items
            item.selected = Chkbool
        Next
    End If
End Sub

回答by Azadeh Khojandi

There is a generic way of having a select all item in asp CheckBoxList with using jquery. You can have as many as CheckBoxList controls on the form with the select all functionality. you only need to make sure

有一种使用 jquery 在 asp CheckBoxList 中选择所有项目的通用方法。您可以在具有全选功能的窗体上拥有多达 CheckBoxList 控件。你只需要确保

  1. Your CheckBoxList has allowSelectAllClass
  2. You added a ListItem to your checkbox list to allow users to select All with the value of All
  1. 您的 CheckBoxList 具有allowSelectAll
  2. 您在复选框列表中添加了一个 ListItem,以允许用户选择 All,其值为All

chkBoxList.Items.Insert(0, new ListItem("All", "All"));

chkBoxList.Items.Insert(0, new ListItem("All", "All"));

you Only need the following code

你只需要以下代码

<script>
    $('.allowSelectAll :checkbox[value=All]').click(function () {
        var toggle = this.checked;
        $(this).closest('.allowSelectAll').find(":checkbox").attr("checked", toggle);
    });
</script>

In the following code spinet I have 4 Checkbox lists

在下面的代码spinet我有4个复选框列表

<div >
<label>Experience 1</label>
<asp:CheckBoxList ID="chklstExp1" runat="server"  CssClass="allowSelectAll">
</asp:CheckBoxList>

<label>Experience 2</label>
<asp:CheckBoxList ID="chklstExp2" runat="server" CssClass="allowSelectAll">
</asp:CheckBoxList>

<label>Experience 3</label>
<asp:CheckBoxList ID="chklstExp3" runat="server" CssClass="allowSelectAll">
</asp:CheckBoxList>

<label>Location</label>
<asp:CheckBoxList ID="chklstLocation" runat="server" CssClass="allowSelectAll">
</asp:CheckBoxList>

<asp:Button runat="server" ID="btnShowReport" OnClick="btnShowReport_Click" Text="Show Report"/>
</div>

回答by user3895427

for(int index = 0; index < checkedListBox.Items.Count; ++index)
{
    checkedListBox.SetItemChecked(index, false);
}

回答by VAMSHI PAIDIMARRI

With this piece of code, you could be able to access the checkbox from C# code behind and could be able to check/uncheck them or even enable/disable them. Hope it might help.

使用这段代码,您可以从后面的 C# 代码访问复选框,并且可以选中/取消选中它们,甚至启用/禁用它们。希望它可能会有所帮助。

foreach (ListItem item in CheckBoxList.Items) {
    item.Selected = true;
    item.Enabled = true;
}

回答by Anu

For making items false you need to do:

要使项目为假,您需要执行以下操作:

checkList.ClearSelection();

For making items as true:

使项目为真:

foreach (var item in checkList.Items.Cast<ListItem>().Where (li => li.Value == "1" || li.Value == "3" || li.Value == "5"))
{
   item.Selected = true;
}

回答by ArunPratap

for Check all

检查所有

foreach (ListItem item in CheckBoxList.Items)
{
    item.Selected = true;    
}

for unchek all

全部取消选中

 CheckBoxList.ClearSelection();