Creating Items at Runtime
- 2 minutes to read
In an Unbound Mode, ASPxMenu allows you to create items with specified settings at runtime, using the MenuItemCollection.Add methods. You can create all the items at one time or create items dynamically on demand.
The code sample below demonstrates how to populate an ASPxMenu control with items manually, in unbound mode. The image below shows the result.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
CreateMenu();
}
}
//Create Menu initial structure.
void CreateMenu() {
string[] Items = { "Home", "News", "Products", "Client Center" };
foreach (string ItemText in Items) {
MyMenu.Items.Add(ItemText);
}
MyMenu.Items.FindByText("News").Items.Add("Subscriptions");
MenuItem[] ProductsItems = {
new MenuItem("Downloads"),
new MenuItem("Support"),
new MenuItem("Order")
};
MyMenu.Items.FindByText("Products").Items.Add(ProductsItems);
}
//Add a new item
protected void btnAddItem_Click(object sender, EventArgs e) {
if ((MyMenu.SelectedItem != null) && (tbItemName.Text != "")) {
MyMenu.SelectedItem.Items.Add(tbItemName.Text);
}
}