2014年7月13日 星期日

[RESOLVED] Checkboxlist like Menu control


I have a menu control in my website where menu items(parent)  and sub menu items(child) are populate through database. In below code ParentId=0 when its a menu item and ParentId > 0 when it's a sub menu item. The code is working fine.


if(!IsPostBack)
{
SqlConnection sqlConnection;

string dbConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
sqlConnection = new SqlConnection(dbConnectionString);
DataSet ds = new DataSet();
DataTable dt = new DataTable();

using (sqlConnection)
{
SqlCommand command = new SqlCommand("proc_GetCategories", sqlConnection);
command.CommandType = CommandType.StoredProcedure;
sqlConnection.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = command;
da.Fill(ds);
dt = ds.Tables[0];
DataRow[] drowpar = dt.Select("ParentId=" + 0);

foreach (DataRow dr in drowpar)
{
menuCategory.Items.Add(new MenuItem(dr["CategoryName"].ToString(),
dr["CategoryID"].ToString()));
}

foreach (DataRow dr in dt.Select("ParentID >" + 0))
{
MenuItem mnu = new MenuItem(dr["CategoryName"].ToString(),
dr["CategoryID"].ToString());
menuCategory.FindItem(dr["ParentID"].ToString()).ChildItems.Add(mnu);
}
}
}

Now in order to insert the menu items in database I want to create a checkboxlist with same parent child relationship as menu-submen control. For example if I have 5 menu items with 3 sub menu items each then in the same way checkboxlist will display 5 parent
check box with 3 checkbox child items each. On select the parent checkbox all child checkbox should be checked.


Please help me in achieving this.




Hi,




anuragrawat123@gmail.com


menu items(parent)  and sub menu items(child) are populate through database


As you have mentioned that you have menu and sub-menu pattern which is being populated from the DB. You might consider using TreeView.




anuragrawat123@gmail.com


Now in order to insert the menu items in database I want to create a checkboxlist with same parent child relationship as menu-submen control. For example if I have 5 menu items with 3 sub menu items each then in the same way checkboxlist will display 5 parent
check box with 3 checkbox child items each. On select the parent checkbox all child checkbox should be checked.


TreeView will help you create the parent child relationship easily using hierarchy and it's also easy to provide checkboxes.


Go to the properties window of the TreeView and find ShowCheckBoxes Property and set it to
Leaf or All.


Leaf will display the checkboxes only for Leaf Nodes and All will display checkboxes for All the nodes. In your case set it to All it will display checkboxes for all Nodes.




On, how to get data from DB and generate TreeView. Refer to the following link.


http://forums.asp.net/t/1978883.aspx?Treeview+Problem




anuragrawat123@gmail.com


On select the parent checkbox all child checkbox should be checked.


For the solution for the last Part that is selecting childNodes on selecting the Parent Node. Use JavaScript. Refer to the following links:


http://pushpontech.wordpress.com/2007/06/06/aspnet-20-treeview-checkbox-checkuncheck-all-script/


http://forums.asp.net/t/1367074.aspx


Hope it helps!


Best Regards!


沒有留言:

張貼留言