Hi,

I am working with the treeview control and binding it dynamically using the treeview_TreeNodePopulate method.

The code works perfectly fine and treeview behaves as it has to.

However, I am facing one annoying issue in the display of the treeview, the last node and its child do not align properly with the top nodes. Check the image below for better understanding,

http://dotnetspider.com/attachments/Forums/235454-281550-tree.GIF


Note: I have blocked the text of options deliberately.

So as you can notice that the child nodes of Level 3 are not getting aligned with that of Level 1 and 2.

Following is my asp code,

<asp:TreeView ID="tvMaster" runat="server" RootNodeStyle-CssClass="rootNode" NodeStyle-HorizontalPadding="2px"
NodeStyle-VerticalPadding="2px" NodeIndent="1" NodeWrap="true" ShowLines="true"
CssClass="tree">
<Nodes>
<asp:TreeNode Text="School Level" PopulateOnDemand="true" SelectAction="Expand">
</asp:TreeNode>
</Nodes>
</asp:TreeView>

In the code behind, PopulateNode event fires for every node and I bind the child nodes for that node dependant on the Business logic.
Any inputs on this issue are appreciated.
Thanks,
Pranil

Can you also post code behind? Looks like terminal condition is not handled properly and instead of adding children to Level 3, Children are being added to root. Also I notice School level is not "Root". Level 1..3 are not children of School level (may be it is by design as per requirements).

Can you also post code behind? Looks like terminal condition is not handled properly and instead of adding children to Level 3, Children are being added to root. Also I notice School level is not "Root". Level 1..3 are not children of School level (may be it is by design as per requirements).

Here you go,

Private Sub tvMaster_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles tvMaster.TreeNodePopulate
        Try
            PopulateNode(e.Node)
        Catch ex As Exception
            MessageControl.SetMessage(ucMessage.MessageType.Failure, ex.Message)
        End Try
    End Sub


Private Sub PopulateNode(ByRef node As TreeNode)
        Dim objSQLCore As SQLCore 
          Dim dsChildNodes As DataSet
        Dim elementMappingId As Integer

            objSQLCore = New SQLCore
        

        Select Case node.Depth
            Case 0
                'To populate the first three nodes
                dsChildNodes = objSQLCore.GetTreeNodes(node.Depth)
                node.Expanded = True
            Case Else
                'To populate child nodes
                elementMappingId = Convert.ToInt32(node.Value.Split(",")(0), CultureInfo.CurrentCulture)
                dsChildNodes = objSQLCore.GetTreeNodes(elementMappingId)
                node.Expanded = False
        End Select

        If Not IsNothing(dsChildNodes) Then
            If dsChildNodes.Tables(0).Rows.Count > 0 Then
                For i As Integer = 0 To dsChildNodes.Tables(0).Rows.Count - 1
                    Dim drChildNodes As DataRow = dsChildNodes.Tables(0).Rows(i)
                    Dim childNode As New TreeNode

                    childNode.Value = Convert.ToString(drChildNodes("TypeMappingId"), CultureInfo.CurrentCulture) + "," + Convert.ToString(drChildNodes("SubjectId"), CultureInfo.CurrentCulture)
                    Select Case node.Depth
                        Case 0
                            childNode.Text = "<strong class='txt11dblue'>" + drChildNodes("Description").ToString + "</strong>"
                        Case 1
                            childNode.Text = "<strong class='txt11pxdgrey'>" + drChildNodes("Description").ToString + "</strong>"
                        Case 2
                            childNode.Text = "<span class='txt11pxdgrey'>" + drChildNodes("Description").ToString + "</span>"
                        Case Else
                            childNode.Text = drChildNodes("Description").ToString
                    End Select

                    childNode.PopulateOnDemand = True
                    childNode.SelectAction = TreeNodeSelectAction.Select

                    node.ChildNodes.Add(childNode)
                    node.SelectAction = TreeNodeSelectAction.Expand
                    node.NavigateUrl = ""
                Next
            End If
        End If
    End Sub

SQLCore is the datalayer class which communicates with the database.

Thanks,

Pranil

Can you also post code behind? Looks like terminal condition is not handled properly and instead of adding children to Level 3, Children are being added to root. Also I notice School level is not "Root". Level 1..3 are not children of School level (may be it is by design as per requirements).

padtes,
Children are being added to Level 3 properly. I can confirm this, since when you click on "+" sign on Level 3 only then, the child nodes show up and "-" sign appears.
also, Level 1..3 are child nodes of School Level.

Thanks,
Pranil

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.