Asp Menu - Binding issue

Posted by Sharpcnet under ASP.NET on 12/19/2013 | Points: 10 | Views : 736 | Status : [Member] | Replies : 3
The asp menu control is in the master page. Its datasource is a web.sitemap file.
This file has all the items/Pages declared as nodes, initially. I have written this code to remove the items from the menu based on the user permissions, after user logs in.

protected void MyMenu_MenuItemDataBound(object sender, MenuEventArgs e) 
{
if(Session["MenuLoaded"]==null)
{
//Code to remove items
}
}

protected void MyMenu_DataBound(object sender, EventArgs e)
{
Session["MenuLoaded"]=true;
}


The reason for the session variable is - The menuitemdatabound happens for every refresh/page request clicks, and I wanted the menu to be loaded only once for a user session.

PROBLEM: The 'Remove Item' code works fine. When the user logs in, the menu items do not show as desired. But when he clicks on an existing item to move to another page, All the menus appear again in the menubar.

Why is this happening. Should I allow the menu items to be bound everytime the page is refreshed or new url requested. its not right.is it?. But any other way around? or i could just remove the session condition.

using C#




Responses

Posted by: vishalneeraj-24503 on: 12/19/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi you can try this
public void DisplayMenuTabs()
{
if (Session["Menu1Access"].ToString() == "True")
Menu1.Visible = true;
else
Menu1.Visible = false;
if (Session["Menu2Access"].ToString() == "True")
Menu2.Visible = true;
else
Menu2.Visible = false;
if (Session["Menu3Access"].ToString() == "True")
Menu3.Visible = true;
else
Menu3.Visible = false;
}

//Or you can find your menu as below
Menu myMenu = (Menu)Master.FindControl("menu1");
myMenu.Visible=false;

//Or you can also try

MenuItemCollection menuItems = mTopMenu.Items;
MenuItem adminItem = new MenuItem();
foreach (MenuItem menuItem in menuItems)
{
if (menuItem.Text == "Roles")
adminItem = menuItem;
}
menuItems.Remove(adminItem);

//Or do the following

MenuItem item = NavigationMenu.FindItem("Users/Manage Accounts");
item.Parent.ChildItems.Remove(item);

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 12/19/2013 [Member] Starter | Points: 25

Up
0
Down
@vishal. Have you even read my question.

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 12/20/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
See the below links:-

http://forums.asp.net/t/1943940.aspx?Data+Bind+to+Menu+Control
http://stackoverflow.com/questions/284398/asp-net-2-0-how-to-bind-an-aspmenu-to-an-sqldatasource

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response