Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Table Driven Menus
Message
From
19/11/2009 01:09:28
 
 
To
18/11/2009 23:54:55
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01435073
Message ID:
01435521
Views:
47
Bernard,

Sorry I didn't flesh it out a little more, but basically what you would do, instead of simply adding a plain old MenuItem, you could have a set of MenuItem sub-classes that would do the work of handling the click event (and any icons you want). For example, I have a menu that allows the user to have a list of Favorites to browse to, so my FavoritesMenuItem looks something like this:
public class FavoritesMenuItem : MenuItem
{
	protected string URL = "";

	public FavoritesMenuItem(string text, string link) : base(text)
	{
		this.Click += new System.EventHandler(this.ClickHandler);
		this.URL = link;
		// this is where you'd do icons too, I don't have any
	}


	#region Events
	protected virtual void ClickHandler(object sender, System.EventArgs e)
	{
		// code here for handling the menu item click to browse the URL
	}
	#endregion
}
Then of course, you'd add them like this:
private void TestMenuAddItemsDynamically()
{
	MenuItem item; // leave it this way so you can have other item sub-classes
	foreach (DataRow row in this.oData.Rows)
	{
		if ((bool)row["IsFavorites"] == true)
		{
		    item = new FavoritesMenuItem(row["description"].ToString(), row["url"].ToString());
		    this.AddMenuItem("Favorites", item);
		}
		else
		{
		    item = new MyOtherMenuItem(row["description"].ToString());
		    this.AddMenuItem("Dynamic Menu", item);
		}
	}
}
Does this help?

~~Bonnie




>>This is pieced together from some stuff I've done (left out some stuff that wasn't easy to tweak for my test purposes to show you a sample). It works, although you will probably want to tweak it.
>>
>>
>>private void TestMenuAddItemsDynamically()
>>{
>>	MenuItem item;
>>	for (int i = 0; i < this.oData.Rows.Count; i++)
>>	{
>>		item = new MenuItem(this.oData.Rows[i]["description"].ToString());
>>		this.AddMenuItem("Dynamic Menu", item);
>>	}
>>}
>>// Generic method for adding to any Menu
>>private void AddMenuItem(string name, MenuItem item)
>>{
>>	string lookForName = name.Replace("&", "");
>>
>>	// Find top-level menu item
>>	MenuItem addTo = null;
>>	for (int i = 0; i < this.Menu.MenuItems.Count; i++)
>>	{
>>		MenuItem topLevel = this.Menu.MenuItems[i];
>>		if (string.Compare(topLevel.Text.Replace("&", ""), lookForName, true) == 0)
>>		{
>>			addTo = topLevel;
>>			break;
>>		}
>>	}
>>
>>	// Was a top-level menu item found?
>>	if (addTo == null)
>>	{
>>		addTo = new MenuItem(name);
>>		this.Menu.MenuItems.Add(addTo);
>>		addTo.Index = this.Menu.MenuItems.Count - 2;
>>	}
>>
>>	addTo.MenuItems.Add(item);
>>}
>>
>>
>>~~Bonnie
>
>Bonnie
>
>From my reading of this code, you add menu items with this code.
>
>But how do you wire in the code/methods to call when that menu item is selected? Seems to me that that bit is missing so all you have is a menu that does nothing when an added item is selected. That would be a pretty useless menu to have.
>
>Please correct me if I am wrong as I am a novice at this.
>
>If data-driving, could you also show how to add an icon (if needed and specified in the table) as well as wiring in code (also in the table) to run when a menu item is selected?
>
>Thanks
>
>Bernard
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform