Click here to Skip to main content
6,595,444 members and growing! (21,161 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

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

By ihatetomato

This article explains dynamic binding to a TreeView control in ASP.NET 2.0 and navigating child nodes using JavaScript.
VB.NET 2.0, WinXP, ASP.NET, VS2005, Dev
Posted:15 Sep 2006
Views:32,270
Bookmarked:7 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.03 Rating: 2.40 out of 5
3 votes, 42.9%
1

2
1 vote, 14.3%
3

4
3 votes, 42.9%
5

Introduction

Here is the code for dynamic binding of a treeview and also the  server side java script for navigating URL to the child nodes.

I have gone through many forums to get the information of the treeview control the only bug i studied is _dopostback javascript that stops the navigating URL of the child nodes.many of them suggested me to better go for autopostproperty to set as true .but the treeview control does'nt have autopostback property.

 the optional way I suggest you  for the child node to navigate URL is by using the java script window.open().

below I have mentioned the source code but you have to write your stored  procedures for ur databases.

And one more requirement to bind a treeview control the table should maintain relations and should posses the unique field  to make relations

also please change the fields i have mentioned in the source code

Dim conn_links As New _
    SqlConnection(ConfigurationManager.AppSettings("DbConnectionString"))

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 += "window.open(""" & 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 _
            SqlDataAdapter("storedprocedure1"conn_links)
        dad_parent.SelectCommand.CommandType = _
            CommandType.StoredProcedure

        '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 = _
                  CommandType.StoredProcedure
        'binding the dataset with table name as parent

        dad_parent.Fill(ds, "parent")
        'disposing the adapter

        dad_parent.Dispose()
        'binding the dataset with table name as child

        dad_child.Fill(ds, "child")
        'disposing the adapter

        dad_child.Dispose()

        '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"), _
           ds.Tables("child").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

            TreeView1.Nodes.Add(masternode)
            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"), _
                    childrow("Link_URL"))
                masternode.ChildNodes.Add(childnode)
            Next
        Next
    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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ihatetomato


Member

Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralMaster Child Relation ship in same table..... PinmemberPradeep K V3:09 5 Oct '07  
Generalnice one! Pinmemberrobocato7:56 5 Jun '07  
Generalformat your post Pinmembercykophysh395:01 15 Sep '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Sep 2006
Editor:
Copyright 2006 by ihatetomato
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project