This is a migrated thread and some comments may be shown as answers.

radmenu by user rights

3 Answers 95 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Emre
Top achievements
Rank 1
Emre asked on 19 Dec 2011, 04:30 PM
hi,

I have a radmenu with sqldatasource.When the user log in the site,the menu will load according to the user rights.I check the user rights from the database.The code below doesn't work for me.How can I achieve this?

private void CreateMenuByUser()
       {
           RadMenu1.Visible = true;
           DataTable table = GetDataTable("SELECT * FROM UserRights INNER JOIN Users ON UserRights.UserID = Users.UserID WHERE Users.UserID = '" + Session["UserID"] + "'");
 
           //RadMenuItemCollection menuItems = RadMenu1.Items;
           RadMenuItem disabledItems = new RadMenuItem();
 
           foreach (DataRow item in table.Rows)
           {
               if (bool.Parse(item["rRead"].ToString()) == false)
               {
                   foreach (RadMenuItem menuItem in RadMenu1.Items)-->>>this line cause the error Index was out of the range error
                   {
                       if (menuItem.Value == item["MenuID"].ToString()) disabledItems = menuItem;
                       if (menuItem.Items.Count > 0) GetAllChildItems(menuItem, item["MenuID"].ToString());
 
                   }
 
                   RadMenu1.Items.Remove(disabledItems);
               }
           }
       }

     private void GetAllChildItems(RadMenuItem itemsList, string MenuId)
      {
           RadMenuItemCollection menuItems = itemsList.Items;
           RadMenuItem disabledItems = new RadMenuItem();
 
           foreach (RadMenuItem menuItem in itemsList.Items)
           {
               if (menuItem.Value == MenuId) disabledItems = menuItem;
           }
 
           menuItems.Remove(disabledItems);
       }

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 20 Dec 2011, 06:36 AM
Hello,

I suppose you want to bind the menu based on login. I have tried the same and here is the sample code.

CS:
protected void Page_Load(object sender, EventArgs e)
   {
       string s = "select * from logintb where username='"+Session["name"]+"' ";
       con.Open();
       SqlDataAdapter dr = new SqlDataAdapter(s, con);
       DataTable dt = new DataTable();
       dr.Fill(dt);
       con.Close();
       menu1.DataSource = dt;
       menu1.DataTextField = "username";
       menu1.DataValueField = "password";
       menu1.DataBind();
   }

-Shinu.
0
Emre
Top achievements
Rank 1
answered on 20 Dec 2011, 02:11 PM
Hello Shinu,

I want to bind the menu based on login,but I also want to hide some menu items from user according to the user type.
0
Emre
Top achievements
Rank 1
answered on 20 Dec 2011, 10:09 PM
I reached the solution by using recursive function.I hope it helps community.

foreach (DataRow item in table.Rows)
           {
               if (bool.Parse(item["rRead"].ToString()) == false)
               {
                   foreach (RadMenuItem menuItem in RadMenu1.Items)
                   {
 
                       if (menuItem.Value == item["MenuID"].ToString()) menuItem.Visible = false;
                       if (menuItem.Items.Count > 0) GetAllChildItems(menuItem, item["MenuID"].ToString());
 
                   }
               }
           }
       }
 
       private void GetAllChildItems(RadMenuItem itemsList, string MenuId)
       {
            
           foreach (RadMenuItem menuItem2 in itemsList.Items)
           {
               if (menuItem2.Value == MenuId) menuItem2.Visible = false;
               if (menuItem2.Items.Count > 0) GetAllChildItems(menuItem2,MenuId);
           }
       }
Tags
Menu
Asked by
Emre
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Emre
Top achievements
Rank 1
Share this question
or