如果选中checkboxlist2中的项目,如何检查checkboxlist1中的项目? [英] How to check a item in checkboxlist1 when a item in checkboxlist2 is checked?

查看:47
本文介绍了如果选中checkboxlist2中的项目,如何检查checkboxlist1中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有2个复选框列表,我从数据库中填充数据。 checkboxlist1获取名称,而checkboxlist2获取该名称的相应roll_number。现在,当我检查checkboxlist1中的第二个名称时,第二个roll_number必须从checkboxlist2中选择。

谢谢

解决方案

让你有一个名为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 xmlns:asp = #unknown > < / asp:checkboxlist >
< h3 > 选择Roll < / h3 >
< asp:checkboxlist id = CheckBoxListRoll autopostback = true onselectedindexchanged = CheckBoxListRoll_SelectedIndexChanged runat = server xmlns:asp = #unknown > < / asp:checkboxlist >
< ; / div >





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





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


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

List< 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);
}
//


}

}

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 2 checkboxlist to which I am populating data from database. The checkboxlist1 get names while the checkboxlist2 get respective roll_number of that names. Now when I check the 2nd name in checkboxlist1, then 2nd roll_number has to get select from checkboxlist2.
Thank You

解决方案

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" 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) 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;

            }
        }
		
		
		}
</student></student>



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


这篇关于如果选中checkboxlist2中的项目,如何检查checkboxlist1中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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