如何使用单独复选框检查所有CheckboxList项目 [英] How do I check all the CheckboxList Items using seperate checkbox

查看:51
本文介绍了如何使用单独复选框检查所有CheckboxList项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。

我有一个CheckBoxList,我从数据库填充数据,这是在Page_Load事件下完成的。现在我需要添加一个名为SelectAll的CheckBox,这样当选中SelectAll CheckBox时,CheckBoxList下的所有项都必须检查而不加载页面。

谢谢。

解决方案

你需要补充的一点是,Page_Load事件的任务应放在if(!IsPostBack)下,然后你的复选框列表不会在每个回发中重新填充。所以你会发现一切正常。但是我在下面添加了一个简单的解决方案



在ASPX中



 <  ![CDATA [<%@       标题  = 主页   语言  =  C#    MasterPageFile   = 〜/ Site.master    AutoEventWireup   =  true  

CodeBehind = Default.aspx.cs 继承 = WebApplication3。 _Default >


< asp :content id = HeaderContent runat = server contentplaceholderid = HeadContent xmlns:asp = #unknown >
< / asp:content >
< asp:content id = BodyContent runat = server contentplaceholderid = MainContent xmlns:asp = #unknown >
< h3 > 选择图书< / h3 >
< span class =code-keyword>< div >
< asp:checkbox id = ckhSelectAll runat = 服务器 text = 全选 oncheckedchanged = ckhSelectAll_CheckedChanged autopostback = true 已检查 = false / >
< asp:checkboxlist id = chkBookList runat = 服务器 > < / asp:checkboxlist >
< span class =code-keyword>< / div >
< / asp:content >



在.cs代码背后



 BooksDBEntities db =  new  BooksDBEntities(); 
受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
List< book> lstBooks = new List< book>();
lstBooks = db.Books.ToList();
foreach (预订b in lstBooks)
{
ListItem li = new ListItem();
li.Text = b.BookName;
li.Value = b.Id.ToString();
chkBookList.Items.Add(li);
}
}

}

受保护 void ckhSelectAll_CheckedChanged( object sender,EventArgs e)
{
if (ckhSelectAll.Checked)
{
foreach (ListItem l in chkBookList.Items)
{
l.Selected = true ;
}
}
其他
{
foreach (ListItem l in chkBookList.Items)
{
l.Selected = false < /跨度>;
}
}
}
< / book > < / book < span class =code-keyword>>


我认为这类问题可能面临很容易,如果你使用ListItem,例如li li.Value和li.Text属性正确。但是我不知道你的情况。我再次附加一个简单的解决方案,你可以通过它自动选择卷号当我从第一个复选框。反之亦然



在ASPX中



 <   div  >  
< h3 > 选择名称< / h3 >
< asp:checkboxlist id = CheckBoxListName autopostback = true onselectedindexchanged = CheckBoxListName_SelectedIndexChanged runat = server xmlns:asp = #unknown > < / asp:checkboxlist >
< h3 > 选择Roll < / h3 >
< asp :checkboxlist i d = CheckBoxListRoll autopostback = true onselectedindexchanged = CheckBoxListRoll_SelectedIndexChanged runat = server xmlns:asp = #unknown > < / asp:checkboxlist >
< / div >





在.cs(代码隐藏)





 BooksDBEntities db =  new  BooksDBEntities(); 
受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{

// 两对复选框
列表< student> lstStudents = new List< student>();
lstStudents = db.Students.ToList();
// 填写姓名复选框
foreach (学生 in lstStudents)
{
ListItem li = new ListItem();
li.Text = s.Name;
li.Value = s.Roll;

CheckBoxListName.Items.Add(li);
}

// 填写滚动复选框

foreach (学生的 in lstStudents)
{
ListItem li = new ListItem();
li.Text = s.Roll;
li.Value = s.Roll;
CheckBoxListRoll.Items.Add(li);
}

//


}
}

受保护 void CheckBoxListName_SelectedIndexChanged ( object sender,EventArgs e)
{
foreach (ListItem li CheckBoxListName.Items中的class =code-keyword>
{

CheckBoxListRoll.Items.FindByValue(li.Value).Selected = li.Selected;


}

}

受保护 void CheckBoxListRoll_SelectedIndexChanged( object sender,EventArgs e)
{

foreach (ListItem li in CheckBoxListRoll.Items)
{
CheckBoxListName.Items.FindByValue(li.Value ).Selected = li。选择;

}
}

< / 学生 > < / 学生 >


Quote:

让你有一个名为Students的数据库表的表如下
ID |卷|姓名
------------------------
1 | 0718004 | Anis
------------------------
2 | 0718003 | Asad
-----------------------
3 | 0718002 | Toma
-----------------------



 In aspx 





 <   div  >  
< < span class =code-leadattribute> h3 > 选择名称< / h3 >
< asp:CheckBoxList ID = CheckBoxListName AutoPostBack = true OnSelectedIndexChanged = CheckBoxListName_SelectedIndexChanged runat = server > < / asp:CheckBoxList >
< h3 > 选择Roll < / h3 >
< asp:CheckBoxList < span class =code-attribute>
ID = CheckBoxListRoll AutoPostBack = true OnSelectedIndexChanged = CheckBoxListRoll_SelectedIndexChanged runat = server > < < span class =code-leadattribute> / asp:CheckBoxList >
< / div >





使用实体框架的.cs(代码隐藏)





< pre lang =c#> public partial class _Default:System.Web.UI.Page
{


BooksDBEntities db = BooksDBEntities();
受保护 void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{

List< Student> ; lstStudents = new 列表< Student>();
lstStudents = db.Students.ToList();
// 填写姓名复选框
foreach (学生 in lstStudents)
{
ListItem li = new ListItem();
li.Text = s.Name;
li.Value = s.Roll;

CheckBoxListName.Items.Add(li);
}

// 填写滚动复选框

foreach (学生的 in lstStudents)
{
ListItem li = new ListItem();
li.Text = s.Roll;
li.Value = s.Roll;
CheckBoxListRoll.Items.Add(li);
}
//


}

}

protected void CheckBoxListName_SelectedIndexChanged ( object sender,EventArgs e)
{
foreach (ListItem li CheckBoxListName.Items中的class =code-keyword>
{

CheckBoxListRoll.Items.FindByValue(li.Value).Selected = li.Selected;


}

}

受保护 void CheckBoxListRoll_SelectedIndexChanged( object sender,EventArgs e)
{

foreach (ListItem li in CheckBoxListRoll.Items)
{
CheckBoxListName.Items.FindByValue(li.Value ).Selected = li。选择;

}
}


}





使用上面的代码,如果你选择了一个代号为0718004的卷号,那么Name的复选框将自动选择Anis,反之亦然。我有链接,其中我提供了您可以下载的完整解决方案。如果它有效,那么upvote并接受为答案



下载项目在这里



谢谢




















Hello.
I have a CheckBoxList to which I am populating data from the database and this is done under Page_Load event. Now I need to add a single CheckBox called SelectAll, so that when SelectAll CheckBox is checked all the items under CheckBoxList has to check without loading the page.
Thank You.

解决方案

One thing you have to add that the task of Page_Load event should be put under if(!IsPostBack) then your checkboxlist won't refill in each postback.So you will find everything work fine.However I have added a simple solution below


In ASPX


<![CDATA[<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

    CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>

<asp:content id="HeaderContent" runat="server" contentplaceholderid="HeadContent" xmlns:asp="#unknown">
</asp:content>
<asp:content id="BodyContent" runat="server" contentplaceholderid="MainContent" xmlns:asp="#unknown">
<h3>Select Books</h3>
<div>
<asp:checkbox id="ckhSelectAll" runat="server" text="Select All" oncheckedchanged="ckhSelectAll_CheckedChanged" autopostback="true" checked="false" />
<asp:checkboxlist id="chkBookList" runat="server"></asp:checkboxlist>
</div>
</asp:content>


In .cs Code behind


 BooksDBEntities db = new BooksDBEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<book> lstBooks = new List<book>();
                lstBooks = db.Books.ToList();
                foreach (Book b in lstBooks)
                {
                    ListItem li = new ListItem();
                    li.Text = b.BookName;
                    li.Value = b.Id.ToString();
                    chkBookList.Items.Add(li);
                }
            }

        }

        protected void ckhSelectAll_CheckedChanged(object sender,EventArgs e)
        {
            if (ckhSelectAll.Checked)
            {
                foreach (ListItem l in chkBookList.Items)
                {
                    l.Selected = true;
                }
            }
            else
            {
                foreach (ListItem l in chkBookList.Items)
                {
                    l.Selected = false;
                }
            }
        }
</book></book>


I think such type of problem can be faced easily if you use ListItem eg li li.Value and li.Text Property properly .However I don't know your case.I have attached a simple solution again for you by which you can select the roll number automatically when I check a name from 1st checkboxlist. and vice-versa 


In ASPX


<div>
<h3>Select Name </h3>
<asp:checkboxlist id="CheckBoxListName" autopostback="true" onselectedindexchanged="CheckBoxListName_SelectedIndexChanged" runat="server" xmlns:asp="#unknown"></asp:checkboxlist>
<h3>Select Roll</h3>
<asp:checkboxlist id="CheckBoxListRoll" autopostback="true" onselectedindexchanged="CheckBoxListRoll_SelectedIndexChanged" runat="server" xmlns:asp="#unknown"></asp:checkboxlist>
</div>



In .cs(Code behind)



        BooksDBEntities db = new BooksDBEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
 if (!IsPostBack)
            {
               
                //Two pair checkboxlist
                List<student> lstStudents = new List<student>();
                lstStudents = db.Students.ToList();
                //filling Names checkboxlist
                foreach (Student s in lstStudents)
                {
                    ListItem li = new ListItem();
                    li.Text = s.Name;
                    li.Value = s.Roll;

                    CheckBoxListName.Items.Add(li);
                }

                //filling Roll Checkboxlist

                foreach (Student s in lstStudents)
                {
                    ListItem li = new ListItem();
                    li.Text = s.Roll;
                    li.Value = s.Roll;
                    CheckBoxListRoll.Items.Add(li);
                }

                //


            }
}

protected void CheckBoxListName_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem li in CheckBoxListName.Items)
            {
               
                    CheckBoxListRoll.Items.FindByValue(li.Value).Selected = li.Selected;
          
            
            }
        
        }

        protected void CheckBoxListRoll_SelectedIndexChanged(object sender, EventArgs e)
        {

            foreach (ListItem li in CheckBoxListRoll.Items)
            {
                CheckBoxListName.Items.FindByValue(li.Value).Selected = li.Selected;

            }
        }

</student></student>


Quote:

Let you have a table having data database table named Students as follows
ID   |	Roll     | Name
------------------------
1    |	0718004	 | Anis
------------------------
2    | 0718003   | Asad
-----------------------
3    | 0718002   | Toma
-----------------------


In aspx



<div>
<h3>Select Name </h3>
<asp:CheckBoxList ID="CheckBoxListName" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxListName_SelectedIndexChanged" runat="server"></asp:CheckBoxList>
<h3>Select Roll</h3>
<asp:CheckBoxList ID="CheckBoxListRoll" AutoPostBack="true" OnSelectedIndexChanged="CheckBoxListRoll_SelectedIndexChanged" runat="server"></asp:CheckBoxList>
</div>



In .cs (Code behind) using Entity Framework



public partial class _Default : System.Web.UI.Page
  {


    BooksDBEntities db = new BooksDBEntities();
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {

              List<Student> lstStudents = new List<Student>();
              lstStudents = db.Students.ToList();
              //filling Names checkboxlist
              foreach (Student s in lstStudents)
              {
                  ListItem li = new ListItem();
                  li.Text = s.Name;
                  li.Value = s.Roll;

                  CheckBoxListName.Items.Add(li);
              }

              //filling Roll Checkboxlist

              foreach (Student s in lstStudents)
              {
                  ListItem li = new ListItem();
                  li.Text = s.Roll;
                  li.Value = s.Roll;
                  CheckBoxListRoll.Items.Add(li);
              }
              //


          }

      }

       protected void CheckBoxListName_SelectedIndexChanged(object sender, EventArgs e)
      {
          foreach (ListItem li in CheckBoxListName.Items)
          {

                  CheckBoxListRoll.Items.FindByValue(li.Value).Selected = li.Selected;


          }

      }

      protected void CheckBoxListRoll_SelectedIndexChanged(object sender, EventArgs e)
      {

          foreach (ListItem li in CheckBoxListRoll.Items)
          {
              CheckBoxListName.Items.FindByValue(li.Value).Selected = li.Selected;

          }
      }


      }



Using above code if you select a roll number say 0718004 then the in checkboxlist for Name will automatically select Anis and vice versa. I have link wherein I provided the complete solution you can download. If it works then upvote and accept as answer 


Download the project here

Thanks











这篇关于如何使用单独复选框检查所有CheckboxList项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆