I'm trying to generate dynamic nested CheckBoxLists in .NET using C#.
I want to display departments and areas with a checkbox next to each. When the checkbox is checked for a department, it should then display the areas for that department right below it, indented, with checkboxes next to them.
I'm able to generate the departments with checkboxes dynamically, but I'm not sure how to go about doing the areas. Part of the issue is that I don't have individual IDs for each department, it's just a list.
<div class="TABLE">
<div class="ROW">
<div class="CELL CELL50">
<asp:CheckBoxList id="cblDepts" runat="server" OnSelectedIndexChange="cblDepts_OnSelectedIndexChange"></asp:CheckBoxList>
</div>
<div class="CELL CELL50">
<asp:CheckBoxList id="cblAreas" runat="server" Visible="false">
</asp:CheckBoxList>
</div>
</div>
</div>
protected void LoadFilters()
{
_datalayer = new DataLayer();
deptAreas = new Dictionary<string, Tuple<string, string>>();
Dictionary<string, string> depts = new Dictionary<string, string>();
DataSet dsDepts = _datalayer.GetDepartments();
if(dsDepts != null && dsDepts.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in dsDepts.Tables[0].Rows)
{
depts.Add(dr["DepartmentId"].ToString(), dr["DepartmentDetails"].ToString());
}
}
this.cblDepts.Items.Clear();
this.cblAreas.Items.Clear();
foreach(var dept in depts)
{
this.cblDepts.Items.Add(new ListItem(dept.Value, dept.Key));
DataSet dsAreas = _datalayer.GetAreas(dept.Key.ToChange<int>().Value);
if(dsAreas != null && dsAreas.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in dsAreas.Tables[0].Rows)
{
deptAreas.Add(dept.Key, Tuple.Create(dr["AreaId"].ToString(), dr["AreaDetails"].ToString()));
}
}
}
}
The result should be that when you click the checkbox next to a department, the areas for that department are displayed below that department, indented, with a checkbox next to each.