How do I add WebDataMenuItem dynamically to a webdatamenu control during load?
Thanks
Hello,
The first thing to know is that you can only add dynamic items to a non-bound WebDataMenu. If you have a WebDataMenu which is bound to some data source, the best way to achieve that is to manipulate the data source itself.
Here is how to dynamically add items to a non-bound WebDataMenu:If we assume we have this menu defined in the markup (.aspx file):
1 <ig:WebDataMenu ID="menu1" runat="server">
2 <Items>
3 <ig:DataMenuItem Text="R2/D2" >
4 <Items>
5 <ig:DataMenuItem Text="Robo R2" />
6 <ig:DataMenuItem Text="Robo D2" />
7 </Items>
8 </ig:DataMenuItem>
9 <ig:DataMenuItem Text="Root Item">
10 <Items>
11 <ig:DataMenuItem Text="Child Item 1" />
12 <ig:DataMenuItem Text="Child Item 2" />
13 </Items>
14 </ig:DataMenuItem>
15 <ig:DataMenuItem Text="Root Item 1">
16 <Items>
17 <ig:DataMenuItem Text="Child Item 1">
18 </ig:DataMenuItem>
19 <ig:DataMenuItem Text="Child Item 2">
20 </ig:DataMenuItem>
21 </Items>
22 </ig:DataMenuItem>
23 </Items>
24 </ig:WebDataMenu>
Then we can use the Page Load event to add items dynamically in the following way:
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 this.menu1.Items[0].Items.Add(
4 new DataMenuItem { Text = "Dynamic Robo at last position" });
5 this.menu1.Items[0].Items.Insert(0,
6 new DataMenuItem { Text = "Dynamic at first position" });
7 this.menu1.Items[2].Items.Add(
8 new DataMenuItem { Text = "Dynamic Child" });
9 }
Please note the the WebDataMenu object, as well as any DataMenuItem object does have an Items collection (of type DataMenuItemsCollection). This is zero-based list of WebDataMenuItem objects which you can manipulate.
Hello
1 protected void Page_Load(object sender, EventArgse)
4 new DataMenuItem { Text = "Dynamic Robo at last position" }); ----- throws an error saying
ARUGMENTOUTOF RANGEEXCEPTION WAS HANDLEDBY USER CODE
INDEX WAS OUT OF RANGE. MUST BE NON-NEGATIVE AND LESS THAN THE SIZE OF THE COLLECTION
NEED SOME HELP IN SOLVING THIS ISSUE
THANKS