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)
Dim strScript As String = "<script language="JavaScript">"
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 (Not IsPostBack) Then
Dim ds As New DataSet
Dim dad_parent As New _
SqlDataAdapter("storedprocedure1"conn_links)
dad_parent.SelectCommand.CommandType = _
CommandType.StoredProcedure
Dim dad_child As New _
SqlDataAdapter("stored procedure2", conn_links)
dad_child.SelectCommand.CommandType = _
CommandType.StoredProcedure
dad_parent.Fill(ds, "parent")
dad_parent.Dispose()
dad_child.Fill(ds, "child")
dad_child.Dispose()
ds.Relations.Add("Children", _
ds.Tables("parent").Columns("Link_id"), _
ds.Tables("child").Columns("Link_id"))
Dim masterrow As DataRow
Dim childrow As DataRow
For Each masterrow In ds.Tables("parent").Rows
Dim masternode As New TreeNode(masterrow("title"))
TreeView1.Nodes.Add(masternode)
masternode.SelectAction = TreeNodeSelectAction.Expand
For Each childrow In masterrow.GetChildRows("Children")
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
Call messagebox(TreeView1.SelectedNode.Value)
End Sub
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.