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






2.55/5 (8 votes)
This article explains dynamic binding to a TreeView control in ASP.NET 2.0 and navigating child nodes using JavaScript.
Introduction
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 window.open()
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 _
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