选择复选框列表中的所有复选框

本文关键字:复选框 列表 选择 | 更新日期: 2023-09-27 18:26:43

我的网页上有一个CheckBoxCheckBox列表
如果选择了CheckBox,则应选择CheckBoxList中的所有CheckBoxes;如果未选中CheckBox,则类似地,应取消选择(未选中)CheckBox中的所有CheckBoxes

.aspx代码

<asp:CheckBoxList ID="CheckBoxList1" runat="server" 
            RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem>Item A</asp:ListItem>
            <asp:ListItem>Item B</asp:ListItem>
            <asp:ListItem>Item C</asp:ListItem>
            <asp:ListItem Selected="True">Item D</asp:ListItem>
            <asp:ListItem>Item E</asp:ListItem>
            <asp:ListItem>Item F</asp:ListItem>
            <asp:ListItem>Item G</asp:ListItem>
        </asp:CheckBoxList>
<asp:CheckBox ID="allChkBox" Text="Select all" runat="server" 
                oncheckedchanged="allChkBox_CheckedChanged" />

我试着做这样的事情,但没有成功:

bool prevSelection = false;
protected void allChkBox_CheckedChanged(object sender, EventArgs e)
    {
if (!prevSelection)
        {
            foreach (ListItem chkitem in CheckBoxList1.Items)
            {
                chkitem.Selected = true;
            }
        }
        else
        {
            foreach (ListItem chkitem in CheckBoxList1.Items)
            {
                chkitem.Selected = false;
            }
        }
        prevSelection = !prevSelection;
}

选择复选框列表中的所有复选框

我更喜欢使用客户端脚本进行类似的操作,这样您的页面就不必进行回发

如果有可能的话,请尝试在单击时启动javascript函数来执行循环并选择。。。类似的东西

<script type="text/javascript">
checked=false;
function checkedAll (frm1) {
    var aa= document.getElementById('frm1');
     if (checked == false)
          {
           checked = true
          }
        else
          {
          checked = false
          }
    for (var i =0; i < aa.elements.length; i++) 
    {
           if(aa.elements[i].type == 'checkbox') { 
             aa.elements[i].checked = checked;
           }
    }
 }
</script>

我已经有一段时间没有接触过ASP.NET了,但每次请求时,您的prevSelection字段都会初始化为false。该值不会在请求之间持久化。因此,您要么需要将其存储在View State或缓存中,然后从那里将其加载到事件处理程序中,或者,更好的是,将您的方法更改为这样的方法:

protected void allChkBox_CheckedChanged(object sender, EventArgs e)
{
    foreach (ListItem chkitem in CheckBoxList1.Items)
    {
        chkitem.Selected = allChkBox.Selected;
    }
}

如果我已经理解了需求,这个怎么样?)?这将使CheckBoxList控件中的所有项目在呈现时默认为selected

protected void Page_Load(object sender, EventArgs e)
{
  if (Page.IsPostBack) return;
  LoadCountryList();
}
private void LoadCountryList()
{
  _ctx = new PayLinxDataContext();
  chkCountries.DataSource = _ctx.Countries.OrderBy(c => c.Name);
  chkCountries.DataBind();
  foreach (ListItem item in chkCountries.Items)
  {
    item.Selected = true;
  }
}

与其在函数外使用变量,不如使用复选框本身:

protected void allChkBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chkbox = sender;
    foreach (ListItem chkitem in CheckBoxList1.Items)
    {
        chkitem.Selected = chkbox.Selected;
    }
}

您可以使用类似的linq

var allChecked = (from ListItem item in CheckBoxList1.Items 
                              where item.Selected 
                              select int.Parse(item.Value)).ToList();

  var all = (from ListItem item in CheckBoxList1.Items 
                                  select int.Parse(item.Value)).ToList();
function CheckUnCheckAll()
    {
        var list = document.getElementById("<%=DataList1.ClientID%>") ;
        var chklist = list.getElementsByTagName("input");
        for (var i=0;i<chklist.length;i++)
        {
            if (chklist[i].type=="checkbox" )
            {
                chklist[i].checked = checkoruncheck;
            }
        } 
    }