Sunday, September 21, 2008

Programmatically add nodes to a WebTree and items to a WebMenu

1. Add a WebTree and WebMenu to the webpage. Give the WebTree an ID of treeAddCollections and the WebMenu an ID of menuAddCollections.
2. Create variables that will be used to create the nodes and items. The Node and Item classes are found in the Infragistics.WebUI.UltraWebNavigator namespace.

Node rootNode;
Node childNode;
Item topItem;
Item subItem;


// Create 5 root nodes with 5 child nodes each.
// By adding to an individual node's Nodes collection, you
// can create more levels to the tree
for (int i = 0; i < 5; i++)
{
rootNode = new Node();
rootNode.Text = "Root Node " + i.ToString();

for (int j = 0; j < 5; j++)
{
childNode = new Node();
childNode.Text = "Child node " + i.ToString() + " " + j.ToString();
rootNode.Nodes.Add(childNode);
}

treeAddCollections.Nodes.Add(rootNode);
}

// Create 5 top level items with 5 subitems each.
// By adding to an individual item's Items collection, you
// can create more levels to the menu
for (int l = 0; l < 5; l++)
{
topItem = new Item();
topItem.Text = "Top Item " + l.ToString();

for (int m = 0; m < 5; m++)
{
subItem = new Item();
subItem.Text = "Sub Item " + l.ToString() + " " + m.ToString();
topItem.Items.Add(subItem);
}

menuAddCollections.Items.Add(topItem);
}

No comments: