Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Treeview duplicates nodes on postback

Posted on 2007-10-06
6
Medium Priority
?
913 Views
Last Modified: 2013-11-07
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
Comment
Question by:rajesh_chd
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:rbharany1
ID: 20031638
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
 
LVL 2

Author Comment

by:rajesh_chd
ID: 20032109
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
 
LVL 2

Accepted Solution

by:
rajesh_chd earned 0 total points
ID: 20033058
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
 
LVL 1

Expert Comment

by:modus_operandi
ID: 20062996
Closed, 250 points refunded.
modus_operandi
Community Support Moderator
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 20062997
Closed, 250 points refunded.
modus_operandi
Community Support Moderator
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question