Creating Groups and Items at Runtime

  • 2 minutes to read

In an Unbound Mode, ASPxNavBar allows you to create groups and items with specified settings at runtime, using the NavBarGroupCollection.Add and NavBarItemCollection.Add methods respectively. You can create all structure at one time, or create groups and items dynamically on demand.

The code sample below demonstrates how to populate an ASPxNavBar control with groups and items manually, in unbound mode. The image below shows the result.

ASPxNavBar_AddItems

protected void Page_Load(object sender, EventArgs e) {
     if (!IsPostBack) {
            CreateNavBar();
     }
}

//Create NavBar initial structure. 
void CreateNavBar() {
     string[] GroupTexts = { "Home", "News", "Products", "Client Center" };
     foreach (string Text in GroupTexts) {
          MyASPxNavBar.Groups.Add(Text);
     }
     MyASPxNavBar.Groups.FindByText("News").Items.Add("Subscriptions");
     NavBarItem[] ProductsItems = { 
          new NavBarItem("Downloads"), 
          new NavBarItem("Support"), 
          new NavBarItem("Order") 
     };
     MyASPxNavBar.Groups.FindByText("Products").Items.Add(ProductsItems);
}

//Add a new group
protected void btnAddGroup_Click(object sender, EventArgs e) {
     if (tbItemName.Text != "") {
          MyASPxNavBar.Groups.Add(tbItemName.Text);
     }
}

//Add a new item
     protected void btnAddItem_Click(object sender, EventArgs e) {
        if ((MyASPxNavBar.ActiveGroup != null) && (tbItemName.Text != "")) {
            MyASPxNavBar.ActiveGroup.Items.Add(tbItemName.Text);
     }
}