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

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);
                        }
                    }
Posted

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