Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted

1 solution

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


ASP
<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


C#
  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
 
Share this answer
 
Comments
partha143 29-Jan-15 2:00am    
Thanks a lot sir. I had missed to set autopostback property from both the checkboxlist. Now my problem is solved. Thanks a lot.
Anisuzzaman Sumon 29-Jan-15 2:23am    
Upvote Plz...
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900