radmenu by user rights

4 posts, 0 answers
  1. Emre
    Emre avatar
    28 posts
    Member since:
    Dec 2011

    Posted 19 Dec 2011 Link to this post

    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);
           }
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Dec 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Emre
    Emre avatar
    28 posts
    Member since:
    Dec 2011

    Posted 20 Dec 2011 Link to this post

    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.
  5. Emre
    Emre avatar
    28 posts
    Member since:
    Dec 2011

    Posted 20 Dec 2011 Link to this post

    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);
               }
           }
Back to Top