• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 920
  • Last Modified:

Treeview duplicates nodes on postback

I am using treeview 2.0 in a program.  It is working fine except one problem.  When I click on a node, it makes a round to server and brings the data which I show to user.  So far it is ok.  But problem begins when its expanddepth property is set to 0 and click on a node at level 1.  It duplicates all the nodes on level 1.  If all the nodes of treeview are expanded then this problem is  not encountered.

Thanks in advance for your help

here is the code to populate the treeview

1. on page_load bind root level elements

        If Not Page.IsPostBack Then
            PopulateRootLevel()
        End If

2.  This code actually binds the root level nodes

    Private Sub PopulateRootLevel()

        Dim objConn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("strconnection").ToString)
        Dim msql As String = "pr_bl_getrootnodes"
        Dim objCommand As New SqlCommand(msql, objConn)
        objCommand.CommandType = CommandType.StoredProcedure
        objCommand.Parameters.Add("@userid", SqlDbType.Int).Value = Convert.ToInt32(Session("userid"))
        objCommand.Parameters.Add("@isAdmin", SqlDbType.Bit).Value = IIf(TextAds.TextAds_Helper.bIsAdmin = True, 1, 0)
       
        Dim da As New SqlDataAdapter(objCommand)
        Dim dt As New DataTable()
        da.Fill(dt)

        PopulateNodes(dt, TreeView1.Nodes)
        da.Dispose()
        dt.Clear()
        dt.Dispose()
    End Sub

3.  This code binds the sub level nodes

    Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal parentNode As TreeNode)

        Dim objConn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("strconnection").ToString)
        Dim objCommand As New SqlCommand("pr_bl_getchildnodes", objConn)
        objCommand.CommandType = CommandType.StoredProcedure
        objCommand.Parameters.Add("@userid", SqlDbType.Int).Value = Convert.ToInt32(Session("userid"))
        objCommand.Parameters.Add("@isAdmin", SqlDbType.Bit).Value = IIf(TextAds.TextAds_Helper.bIsAdmin = True, 1, 0)
        objCommand.Parameters.Add("@parentid", SqlDbType.Int).Value = parentid

        Dim da As New SqlDataAdapter(objCommand)
        Dim dt As New DataTable()
        da.Fill(dt)

        PopulateNodes(dt, parentNode.ChildNodes)
        da.Dispose()
        dt.Clear()
        dt.Dispose()
    End Sub

4.  common code to make nodes

    Private Sub PopulateNodes(ByVal dt As DataTable, ByVal nodes As TreeNodeCollection)

        For Each dr As DataRow In dt.Rows
            Dim tn As New TreeNode()
            tn.Text = dr("category").ToString()
            tn.Value = dr("id").ToString()
            'tn.NavigateUrl = "javascript:void(0);"
           
            If Convert.ToInt16(dr("isfolder")) = 1 Then
                tn.ImageUrl = "images/folder.png"
            Else
                tn.ImageUrl = "images/page.png"
            End If
           
            nodes.Add(tn)

            'If node has child nodes, then enable on-demand populating
            tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
        Next
        dt.Clear()
        dt.Dispose()
        'nodes.Clear()
    End Sub

5.  Other treeview events running on the page

    Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
        PopulateSubLevel(CInt(e.Node.Value), e.Node)
    End Sub
   
   
    Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
        If Me.TreeView1.SelectedNode.ChildNodes.Count > 0 Then
            GetPostForEdit(Me.TreeView1.SelectedValue, True)
        Else
            GetPostForEdit(Me.TreeView1.SelectedValue, False)
        End If
        updpCats.Update()
        updpNewPost.Update()
    End Sub


6. and this is treeview define on the form

                                    <asp:TreeView ID="TreeView1" ExpandDepth="9" PopulateNodesFromClient="true" ShowLines="true" ShowExpandCollapse="true" runat="server" ImageSet="inbox" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" />


0
rajesh_chd
Asked:
rajesh_chd
  • 2
  • 2
1 Solution
 
rbharany1Commented:
Hi Rajesh

Can you post the code for the function GetPostForEdit() becuase it sounds like this issue is occuring in the logic of this function.
If it was in any of the other methods it would have showed up on the page Load event.
However as I understand this problem is occuring on the node click event it sounds like its firing the Selected_node changed Event
0
 
rajesh_chdAuthor Commented:
Here is the code for GetPostForEdit procedure

  Private Sub GetPostForEdit(ByVal id As Int32, ByVal isFolder As Boolean)
        Dim db As New DAL.DBtextAds
        Dim msql As String = ""
        If isFolder Then
            msql = "SELECT ID, CATID, POST_TITLE, POST_DATE, POST, ISDEFAULT FROM BL_POSTS WHERE CATID = " & id & " AND ISDEFAULT = '1'"
        Else
            msql = "SELECT ID, CATID, POST_TITLE, POST_DATE, POST, ISDEFAULT FROM BL_POSTS WHERE ID = " & id
        End If
       
        Dim dr As Data.SqlClient.SqlDataReader
        dr = db.ExecuteReader(msql, True)
        If dr.Read Then
            Me.lstCats.SelectedValue = dr("catid")
            Me.txtTitle.Text = dr("post_title")
            Me.txtPost.Value = dr("post").ToString.Replace(Chr(34) & Chr(34), Chr(34))
            Me.txtPostDate.Text = dr("post_date").ToString
            Me.chkDefault.Checked = Convert.ToBoolean(dr("isdefault"))
        End If
    End Sub


Thanks

Rajesh
0
 
rajesh_chdAuthor Commented:
ok this worked for me,

    Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
        If e.Node.PopulateOnDemand = True Then
            PopulateSubLevel(CInt(e.Node.Value), e.Node)
        End If
    End Sub


I request admin to please close this question.

Thanks
0
 
modus_operandiCommented:
Closed, 250 points refunded.
modus_operandi
Community Support Moderator
0
 
modus_operandiCommented:
Closed, 250 points refunded.
modus_operandi
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now