Click here to Skip to main content
13,901,946 members
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 15 Sep 2006
Licenced CPOL

Dynamic binding to a TreeView control in ASP.NET 2.0 and navigating child nodes with JavaScript

, 15 Sep 2006
Rate this:
Please Sign up or sign in to vote.
This article explains dynamic binding to a TreeView control in ASP.NET 2.0 and navigating child nodes using JavaScript.


Here is the code for dynamic binding of a TreeView and also the server-side JavaScript for the navigating URL to the child nodes.

I have gone through many forums to get information on the TreeView control. The only bug I found is the _dopostback JavaScript that stops the navigating URL of the child nodes. Many suggested it would be to better go for the autopostback property to be set as true. But the TreeView control doesn't have an autopostback property.

An optional way is to use the JavaScript function. I have listed the source code below, but you will have to write your own Stored Procedures for youur databases.

A requirement to bind a TreeView control would be that the table should maintain relations and should possess unique fields to make relations.

Also, please change the fields I have indicated in the source code:

Dim conn_links As New _

Public Function messagebox(ByVal strMessage As String)
' A Public Function for Displaying a MessageBox
'Note : Generates the message:
'Note : Finishes Server Processing, Returns To Client.
    Dim strScript As String = "<script language="JavaScript">"
    'strScript += "alert(""" & strMessage & """);"
    strScript += """" & strMessage & """);"
    strScript += "</script>"
    If (Not ClientScript.IsStartupScriptRegistered("clientScript")) Then
        Page.RegisterStartupScript("clientScript", strScript)
    End If
End Function

Protected Sub Page_Load(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles Me.Load
'if the page is postback before hand the logic 
'will not execute to save the execution time 
'this is useful when a treeview node is selected 
    If (Not IsPostBack) Then
        'declaring a dataset
        Dim ds As New DataSet
        'defining an adapter to invoke a storedproceudre 
        'to retreive the fields of BioInfosite_resourcelinks1_TBL
        Dim dad_parent As New _
        dad_parent.SelectCommand.CommandType = _

        'defining another adapter to invoke a storedprocudure 
        'to retreive the fields of BioInfosite_resourcelinks_TBL
        Dim dad_child As New _
            SqlDataAdapter("stored procedure2", conn_links)
        dad_child.SelectCommand.CommandType = _
        'binding the dataset with table name as parent
        dad_parent.Fill(ds, "parent")
        'disposing the adapter
        'binding the dataset with table name as child
        dad_child.Fill(ds, "child")
        'disposing the adapter

        'defining the relations for the tables present 
        'in the dataset with children as a tablename 
        ds.Relations.Add("Children", _
           ds.Tables("parent").Columns("Link_id"), _
        'declaring a row to bind the parent node 
        Dim masterrow As DataRow
        'declaring a row to bind the child node
        Dim childrow As DataRow
        'loop to run until the master rows 
        'present in the table parent
        For Each masterrow In ds.Tables("parent").Rows
            Dim masternode As New TreeNode(masterrow("title"))
            'binding master row data to the treeview master node
            masternode.SelectAction = TreeNodeSelectAction.Expand

            'loop for binding the child row to treeview 
            'that make a relation to the Link_id field
            For Each childrow In masterrow.GetChildRows("Children")
            'binding the child row to the treeview 
            'with value as the URL of the field
                Dim childnode As New _
                    TreeNode(childrow("Link_Name"), _
    End If
End Sub

Protected Sub TreeView1_SelectedNodeChanged(ByVal sender _
          As Object, ByVal e As System.EventArgs) _
          Handles TreeView1.SelectedNodeChanged
    'calling a javascript function to invoke a new window open
    Call messagebox(TreeView1.SelectedNode.Value)
End Sub


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMaster Child Relation ship in same table..... Pin
Pradeep K V5-Oct-07 2:09
memberPradeep K V5-Oct-07 2:09 
Generalnice one! Pin
robocato5-Jun-07 6:56
memberrobocato5-Jun-07 6:56 
Generalformat your post Pin
Three Nine Consulting15-Sep-06 4:01
professionalThree Nine Consulting15-Sep-06 4:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 15 Sep 2006
Article Copyright 2006 by ihatetomato
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid