Click here to Skip to main content
14,883,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to disable or enable my menu bar for users from database,i have done for parent menu items,but i also want to disable or enable the child menu items,here is my code
C#
StrSql = "Select serial_no,optionName,MenuID from [User_" + Session["UserEmail"] + "]";
                    StrSql = StrSql + " Order By serial_no";
                    DataSet rs = (DataSet)MethodClass.ConnectionToQuery(StrSql);
                    if (rs.Tables[0].Rows.Count > 0)
                    {
                        for (i = 0; i < MnuUserManagement.Items.Count; i++)
                        {
                            StrOpt = MnuUserManagement.Items[i].Text.ToString();
                            if (StrOpt != "-" || StrOpt == "")
                            {
                                for (j = 0; j < rs.Tables[0].Rows.Count ; j++)
                                {
                                    if (StrOpt == rs.Tables[0].Rows[j]["optionName"].ToString())
                                    {
                                        MnuUserManagement.Items[i].Enabled = true;
                                    }
                                }
                            }
                        }
                    }
Posted
Updated 18-Dec-14 23:46pm
v2
Comments
Sinisa Hajnal 19-Dec-14 5:46am
   
Consider using Select on rs.Tables[0] instead of for each since you're iterating over every row for each menu item. Or at least once you find the option, call break; so that the inner loop terminates.

For child items, you have to iterate through child items within j-loop after .Enable = true; (if the given item has subchildren). If you have more levels then you have to write recursive method (you can do it for current problem too).

i have solved it my self, but thanks to all of you guys, solution is:
C#
StrSql = "Select serial_no,optionName,MenuID from [User_" + Session["UserEmail"] + "]";
              StrSql = StrSql + " Order By serial_no";
              DataSet rsmenu = (DataSet)MethodClass.ConnectionToQuery(StrSql);
              if (rsmenu.Tables[0].Rows.Count > 0)
              {

                  for (var i = 0; i < rsmenu.Tables[0].Rows.Count; i++)
                  {
                      foreach (MenuItem item in MnuUserManagement.Items)
                      {
           if (item.Text == rsmenu.Tables[0].Rows[i]["optionName"].ToString())
                          {
                              item.Enabled = true;
//Here i start a new dataset for child menu item
StrSql = "Select serial_no,optionName,MenuID from [User_" + Session["UserEmail"]+ "]";
            StrSql = StrSql + " Order By serial_no";
            DataSet rs = (DataSet)MethodClass.ConnectionToQuery(StrSql);

            if (rs.Tables[0].Rows.Count > 0)
            {
                for (var i = 0; i < rs.Tables[0].Rows.Count; i++)
                {
                    
                    foreach (MenuItem childItem in item.ChildItems)
                    {
              if (childItem.Text == rs.Tables[0].Rows[i]["Optionname"].ToString())
                        {
                            childItem.Enabled = true;
                        }
                    }
                }
            }
                          }
                      }
                  }

              }
   
Store data from datatable in a list
Use List Menu i.e. HTML Menu
use if else condition in aspx page to hide the menu that user does not has the rights
   

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