I have a web application which have a datalist within a datalist. The outer datalist make parent items and the inner one the child items both with checkboxes for selection. I am retrieving these items from a database.
THE PROBLEM:
1. I want the parent item to be checked/unchecked automatically if any of the children is checked/unchecked.
2. All the children should be checked if the parent is checked.
I just dont know how to do this with datalists. Although, I have implemented this logic in my code by the use of flags and count variables but I want it to be visible to users. Shall i use javascripts for that? But keeping in mind tht scripting has issues with browsers. IE is recommended browser.
Many thanks.
Technology Used is .NET 4.
Below is the code I hv used for implementing this logic in C#.
bool flag;
bool Parentflag = false;
int count = 0;//count=1 means only parent exists, count=2 means parent+child exists
int labelID = 0;
using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
BLL.Admin adminObj = new BLL.Admin(sqlcon.ConnectionString);
adminObj.DeletePriorPermissions(DropDownRoles.SelectedValue.ToString());
foreach (DataListItem lstitem in dlstMenu.Items)
{
//Code for iterating Parent Menu Items
if (lstitem.ItemType == ListItemType.Item || lstitem.ItemType == ListItemType.AlternatingItem)
{
flag = false;
Label lblPageIdParent = (Label)lstitem.FindControl("lblPageId");
CheckBox chkPageCaption = (CheckBox)lstitem.FindControl("chkPageCaption");
labelID = Convert.ToInt32(lblPageIdParent.Text);
if (chkPageCaption.Checked)
//Add Permissions for Parent Page
{
adminObj.SavePagePermissions(1, Convert.ToInt32(lblPageIdParent.Text), DropDownRoles.SelectedValue.ToString());
Parentflag = false;
count = 1;
}
DataList dlstpage = (DataList)lstitem.FindControl("dlstpage");
foreach (DataListItem item in dlstpage.Items)
{
Parentflag = true;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
//parent checkbox
CheckBox cbxPage = (CheckBox)item.FindControl("chkPageCaption");
RadioButtonList rdoPermissions = (RadioButtonList)item.FindControl("rdoPermission");
Label lblPageID = (Label)item.FindControl("lblPageId");
if (cbxPage != null)
{
if (cbxPage.Checked)
{
//Check parent if any of the children is checked.
if (!chkPageCaption.Checked)
{
//Save permissions for parent item only once.
if (!flag)
{
adminObj.SavePagePermissions(1, Convert.ToInt32(lblPageIdParent.Text), DropDownRoles.SelectedValue.ToString());
flag = true;
}
}
foreach (ListItem rdlst in rdoPermissions.Items)
{
if (rdlst.Selected)
{
//Save Permissions for child items.
adminObj.SavePagePermissions(Convert.ToInt32(rdlst.Value), Convert.ToInt32(lblPageID.Text), DropDownRoles.SelectedValue.ToString());
Parentflag = true;
count = 2;
}
}
}
}
}
}
}
//Delete entry for parent item if none of the children is selected.
if (count != 0)
{
if (Parentflag==true && count == 1)
adminObj.ExecuteSQL("DELETE FROM [tbl_PagePermissions] WHERE PAGEID = " + labelID + " AND ROLEID = '" + DropDownRoles.SelectedValue.ToString() + "'", sqlcon.ConnectionString);
}
}