I am working on a tree view control for a project that I am currently involved in. This tree view is made of three parts: TreeNode class, TreeNodeCollection class, and TreeView user control. The control has one line of HTML, a div container. When I call a function, I need it to run through the tree node collection recursively and generate the links and buttons necessary. Ok, well I've got the code generating all of the links and buttons, but the actually hiding of the nodes isn't happening because the javascript requires the id of the containing div. I am coding it to take the ClientId of the div as I create it, but it is only giving me the id that fed to the control, and not the one used when the page is generated. This is doing some screwy stuff, so if you want to give me some pointers elsewhere, I'ld definitely appreciate it.
I'm not going to post all of the code, just what I find to be the problem areas.
TreeView.ascx html
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="TreeView.ascx.
vb" Inherits="PeCON.TreeView" TargetSchema="
http://schemas.microsoft.com/intellisense/ie5" %>
<div id="TreeViewContainer" runat="server" style="text-align: left"></div>
TreeView.ascx.vb
Public Sub BuildTreeView()
' call recursive method for displaying nodes
'TreeViewContainer is defined in the Windows Generated Code section but is not instantiated when we come across this code
If IsNothing(TreeViewContaine
r) Then
TreeViewContainer = New Web.UI.HtmlControls.HtmlGe
nericContr
ol("div")
Me.Controls.Add(TreeViewCo
ntainer)
End If
DisplayNodes(_nodes, TreeViewContainer, 0)
End Sub
Private Sub DisplayNodes(ByVal Nodes As TreeNodeCollection, ByVal Container As Web.UI.Control, ByVal Level As Integer)
For i As Integer = 0 To Nodes.Count - 1
Dim hl As New Web.UI.WebControls.HyperLi
nk
hl.Text = Nodes(i).Name
hl.NavigateUrl = Nodes(i).Url
Container.Controls.Add(hl)
Dim newLine As New Web.UI.WebControls.Literal
newLine.Text = "<BR>"
Container.Controls.Add(new
Line)
If Nodes(i).HasChildren Then
Dim btn As New Web.UI.WebControls.Image
btn.ID = "btn" & i.ToString & "_" & Level.ToString
btn.ImageUrl = "..\images\plus.gif"
Container.Controls.Add(btn
)
Dim newDiv As New Web.UI.HtmlControls.HtmlGe
nericContr
ol("div")
newDiv.ID = "div" & i.ToString & "_" & Level.ToString
newDiv.Style.Add("text-ali
gn", "left")
newDiv.Style.Add("margin-l
eft", CStr(Level * 0.2) & "in")
DisplayNodes(Nodes(i).Chil
dren, newDiv, Level + 1)
Container.Controls.Add(new
Div)
btn.Attributes.Add("onclic
k", "ToggleDisplay(this, " & newDiv.ClientID & ")")
End If
Next
End Sub
Generated output:
<div id="mainContent_divContent
" align="center" class="ContentPane">
<img class="ContainerTopLeft" src="images\cont-tl.gif">
<img class="ContainerTopRight" src="images\cont-tr.gif">
<img class="ContainerBottomLeft
" src="images\cont-bl.gif">
<img class="ContainerBottomRigh
t" src="images\cont-br.gif">
<div>
<a href="Design/eater.aspx">D
atabases</
a><BR>
<img id="mainContent_TreeView_b
tn0_0" onclick="ToggleDisplay(thi
s, div0_0)" src="images/plus.gif" alt="" border="0" />
<div id="mainContent_TreeView_d
iv0_0" style="text-align:left;mar
gin-left:0
in;">
<a href="Design/someplace.asp
x">PeCON</
a><BR>
<img id="mainContent_TreeView_b
tn0_1" onclick="ToggleDisplay(thi
s, div0_1)" src="images/plus.gif" alt="" border="0" />
<div id="mainContent_TreeView_d
iv0_1" style="text-align:left;mar
gin-left:0
.2in;">
<a href="Design/blah.aspx">St
ored Procedures</a><BR>
<a href="Design/blah.aspx">Ta
bles</a><B
R>
<a href="Design/blah.aspx">Vi
ews</a><BR
>
<a href="Design/blah.aspx">Us
er Defined Functions</a><BR>
</div>
</div>
</div>
</div>
TreeViewContainer.Id = "TreeViewContainer"
to your BuildTreeView() method like this:
Public Sub BuildTreeView()
' call recursive method for displaying nodes
'TreeViewContainer is defined in the Windows Generated Code section but is not instantiated when we come across this code
If IsNothing(TreeViewContaine
TreeViewContainer = New Web.UI.HtmlControls.HtmlGe
TreeViewContainer.Id = "TreeViewContainer"
Me.Controls.Add(TreeViewCo
End If
DisplayNodes(_nodes, TreeViewContainer, 0)
End Sub