Monday, April 07, 2008

Wow Nice Blog Name

I have know idea how i ended up with this blog name. I know i didn't create it, but since i just happen to know a little asp, I shant let this blog title go to waste.

Today I learned how to dynamically create a menu control reading from the sitemap.config file.
I spent a couple days on it and i think this is the best way to do it.

Dynamically Create Menu Control based on web.sitemap
//*************************************************************************
SiteMapNodeCollection baseCollection = new SiteMapNodeCollection(SiteMap.RootNode);
SiteMapNodeCollection parentCollection = SiteMap.RootNode.ChildNodes;
Menu menu2 = new Menu();
if (Session["User"] != null)
{
LocalUser user = (LocalUser)Session["User"];
int count = 1;
foreach (SiteMapNode parentNode in parentCollection)
{
if (user.AccessLevel >= int.Parse(parentNode.Description))
{
menu2.MenuItemClick += new MenuEventHandler(Menu_MenuItemClick);
MenuItem parentItem = new MenuItem("", "", "~/images/top_" + count.ToString() + "t.gif");
menu2.Items.Add(parentItem);
SiteMapNodeCollection childCollection = parentNode.ChildNodes;
foreach (SiteMapNode childNode in childCollection)
{
menu2.MenuItemClick += new
MenuEventHandler (Menu_MenuItemClick);//reader.MoveToAttribute(i);
MenuItem childItem = new MenuItem(childNode.Title, "", "", childNode.Url);
parentItem.ChildItems.Add(childItem);
}
count++;
}
}
}
//************don't forget to create the event*******************************

protected void Menu_MenuItemClick(object sender, MenuEventArgs e)
{ }
//***************notes****************************************************
I'm building the menu based on the user access level, i placed in the web.sitemap attribute description the level i wanted the parent to have.

0 Comments:

Post a Comment

<< Home