![]() |
Web Development »
ASP.NET »
General
Intermediate
Dynamic binding to a TreeView control in ASP.NET 2.0 and navigating child nodes with JavaScriptBy ihatetomatoThis 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
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
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
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 15 Sep 2006 Editor: |
Copyright 2006 by ihatetomato Everything else Copyright © CodeProject, 1999-2009 Web11 | Advertise on the Code Project |