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();
foreach (Student s in lstStudents)
{
ListItem li = new ListItem();
li.Text = s.Name;
li.Value = s.Roll;
CheckBoxListName.Items.Add(li);
}
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