Proble with TreeView IE Web Controls / Database Bound

M

Mike

I am trying to add a node under existing nodes, but making sure they don't
exist before putting adding it.

Here is my Code



Public Sub BuildTree()
.... above this, root nodes were added.
For Each dr In drs
Dim childnode As TreeNode = nTreeNode(dr("ROLE_DESC"),
dr("ROLE_ID"), dr("REL_ROLE_ID"))
Dim parentnode As TreeNode =
treeRoles.FindControl(dr("REL_ROLE_ID"))
If treeRoles.Nodes.Contains(childnode) = False Then
treeRoles.Nodes.AddAt(treeRoles.Nodes.IndexOf(parentnode),
childnode)
End If
Next

End Sub

Public Function nTreeNode(ByVal strText As String, ByVal strID As String,
ByVal strNodeData As Object) As TreeNode

Dim tn As New TreeNode
tn.Text = strText
tn.ID = strID
If IsDBNull(strNodeData) = False Then
tn.NodeData = strNodeData
Else
tn.NodeData = ""
End If

Return tn

End Function




I am trying to set parentnode as a reference to the parent node. However,
the FindControl method cannot be converted to a TreeNode type. Is there
another way to do this. This TreeView is bound to a database and all the data
is in one table. The table contain a primaryID (ROLE_ID) and
ParentID(REL_ROLE_ID), where the ParentID is the PrimaryID of the rows
parent.

ie


Table1

ROLE_ID REL_ROLE_ID
1
2 1
3 1
4
5
6 4
7 4
8 7
9 7
10 7



This table should produce the following tree


1
-- 2
-- 3
4
-- 5
-- 6
-- 7
-- -- 8
-- -- 9
-- -- 10



Thanks in advance.
 
S

Steven Cheng[MSFT]

Hi Mike ,

Thanks for your posting. From the code you provided, I've got your
requirement, it seems that you need to build a TreeView from a Database
table and you use a DataReader to retrieve the rows ,Yes? After that , you
loop through all the rows and add nodes according the rows , but you meet
problem when trying to locate a certain node's Parent Node via its id, yes?

Based on my research, the IE WebControl TreeView only provide the means to
locate a certain node via its index number by the "GetNodeFromIndex"
method. For example if I have a treeView which has the following strucuture

treeRoles/
pr1/
cr11
cr12
cr13
pr2/
cr21
cr22
cr23
pr3/
cr31
cr32
cr33

Then
treeRoles.GetNodeFromIndex("0.1") return node "cr12"
treeRoles.GetNodeFromIndex("0.2") return node "cr13"
treeRoles.GetNodeFromIndex("2.0") return node "cr31"
treeRoles.GetNodeFromIndex("2.2") return node "cr33"

Also, the FindControl is a general method of the System.Web.UI.Control
class which won't help for this problem.

So my suggestion is that we can consider using DataTable rather than
DataReader to retrieve the datarows f rom database and we can use
DAtaTable.Select method to retrieve all the parent roles first and build
the parent nodes , and when adding each parent node, we select the child
roles from the DataTable and add them at the same time. Here is a simple
demo page I've made, you can have a look to see whethe this means is ok:


#only codebehind is paste here since the aspx page only contains a TreeView
Control named "treeRoles"
==================================
Imports Microsoft.Web.UI.WebControls

Public Class DBTree
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents treeRoles As Microsoft.Web.UI.WebControls.TreeView

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here



If Not IsPostBack Then
BuildTree()
End If

Dim node As TreeNode = treeRoles.GetNodeFromIndex("0.4")
Response.Write("<br>" & node.Text)
End Sub



Private Sub BuildTree()

Dim roles As DataTable = GetRoleTable()
Dim proles() As DataRow = roles.Select("REL_ROLE_ID = 'ROOT'")

Dim i As Integer

For i = 0 To proles.Length - 1
Dim tn As New TreeNode
tn.ID = proles(i)("ROLE_ID")
tn.Text = proles(i)("ROLE_NAME")

treeRoles.Nodes.Add(tn)

BuildSubTree(roles, tn)
Next


End Sub

Private Sub BuildSubTree(ByVal roles As DataTable, ByVal parent As
TreeNode)

Dim subroles() As DataRow = roles.Select("REL_ROLE_ID ='" &
parent.ID & "'")

If subroles Is Nothing Or subroles.Length = 0 Then

Return

End If


Dim i As Integer

For i = 0 To subroles.Length - 1

Dim tn As New TreeNode
tn.ID = subroles(i)("ROLE_ID")
tn.Text = subroles(i)("ROLE_NAME")

parent.Nodes.Add(tn)

BuildSubTree(roles, tn)

Next

End Sub

Private Function GetRoleTable() As DataTable

Dim dt As DataTable

dt = New DataTable("Roles")
dt.Columns.Add("ROLE_ID", GetType(String))
dt.Columns.Add("REL_ROLE_ID", GetType(String))
dt.Columns.Add("ROLE_NAME", GetType(String))

Dim i As Integer
Dim j As Integer

Dim pdr As DataRow
For i = 1 To 6

pdr = dt.NewRow()
pdr("ROLE_ID") = "PARENT_ROLE_" & i
pdr("REL_ROLE_ID") = "ROOT"
pdr("ROLE_NAME") = "PARENT_ROLE_" & i

dt.Rows.Add(pdr)

Next


Dim sdr As DataRow
Dim prid As Integer

For j = 1 To 30

Dim rand As New Random(j)
prid = rand.Next() Mod 6


sdr = dt.NewRow()
sdr("ROLE_ID") = "CHILD_ROLE_" & j
sdr("REL_ROLE_ID") = "PARENT_ROLE_" & prid
sdr("ROLE_NAME") = "CHILD_ROLE_" & j

dt.Rows.Add(sdr)
Next


Return dt

End Function

End Class
==========================================

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

Mike

Steven,

Thanks for the help, adding sub tree with the BuildSubTree did the job.

Thanks, MikeL
 
S

Steven Cheng[MSFT]

You're welcome Mike.

Have a good day!

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top