Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
660
Adding nodes to a tree and binding at same time?
posted

Can you add nodes programatically to a tree as well as bind other levels at the same time?  For example, the first level is a straightforward query that can be bound to the tree, but the second level has to be calculated based on first level nodes, and an intermediate dataset, so there is 2 relationships in between the first and second level nodes of the tree, and the third and fourth levels have only one relationship in between them both.

 

Thanks.

 

Parents
No Data
Reply
  • 1923
    posted

    I've used this concept many times for both the WebMenu and the WebTree...

    I have a single dataset where each row is a menu item or node item...the dataset has a Parent_ID field which has the ID of the parent...the topmost (root) node/items have a parent_id of NULL...

    Then, to create the structure, simply iterate through the dataset...hopefully the following code makes sense...

    1. Create a variable at the top of the class...something like this: (i have a strongly typed dataset called dsMenu with a MENU table in it). E.g:

    Private _dsMenu As dsMenu

     

    2. Add this code to the Page_Load event to begin adding the items to the control

    ' retrieve the data to build the menu
    _dsMenu = ...fill your dataset however you want...

    ' get all the top level nodes by selecting where parent_id is null...e.g. no parents!
    Dim drTopLevel() As dsMenu.MENURow = _dsMenu.MENU.Select("PARENT_ID IS NULL", "SORT_ORDER ASC")

    ' Call the MenuItemAddLoop function to add all the top level nodes...
    MenuItemAddLoop(drTopLevel)

     

    3.  Use this function to loop through a collection of rows...

        Private Sub MenuItemAddLoop(ByVal Children() As dsMenu.MENURow)
            For Each Row As dsMenu.MENURow In Children
                ' Add the row to the tree
                MenuItemAdd(Row)

                ' find children of the current row...
                ' this is where the parent_id is the
                ' row's id...
                Dim Search As String = String.Format("PARENT_ID = {0}", Row.ID)
                Dim childNodes() As dsMenu.MENURow = _dsMenu.MENU.Select(Search, "SORT_ORDER ASC")
               
                ' if this menu item has children, run the loop
                ' for this item as well!
                If childNodes.Length > 0 Then MenuItemAddLoop(childNodes)
            Next
        End Sub

     

    4. Use this function to add the new node to the control...this is just a single place to set up each menu item/node...the dataset row has all the data needed for each menu item...

        Protected Sub MenuItemAdd(ByVal drMenuItem As dsMenu.MENURow)
            Dim lastNode As Item = Nothing
            Dim parentNode As Item = Nothing

            ' create a new menu item
            lastNode = New Item()

            ' set the text for the node
            lastNode.Text = drMenuItem.DESCRIPTION

            ' set the nodes tag. the tag must be
            ' unique. it is the PK of the table
            ' and this will make finding
            ' other nodes very easy!
            lastNode.DataKey = drMenuItem.ID
            lastNode.Tag = drMenuItem.ID

            ' check whether this is a root level menu item
            If drMenuItem.IsPARENT_IDNull Then
                ' nodes parent is null...add it directly
                ' to the tree so as to be a root level
                ' node...
                UltraWebMenu1.Items.Add(lastNode)

            Else
                ' node has a parent. find the parent
                ' in the tree using the PK/tag and
                ' then add it to the parents nodes
                ' collection (its children)
                parentNode = uwm_main.Find(drMenuItem.PARENT_ID)
                parentNode.Items.Add(lastNode)

            End If

        End Sub

     

    i can explain more if necessary!

Children
No Data