Click here to Skip to main content
15,888,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Experts,

I have 2 user controls (UCs), each contains a treeview. These 2 UCs are placed on the main aspx page. When clicking on a node from the one UC, I want the treeview on the 2nd UC to change the contents. How can this be done? Thanks!
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jul-15 20:16pm    
What's the problem? Any particular detail of such population?
—SA

1 solution

1. Handle UC1's SelectedNodeChanged event and try to find the second user control in the page.
2. For each child control in the second user control check if you find treeview control.
3. If you did, add/delete the elements programmatically.
4. Sample code
ControlCollection ctl = Parent.Controls;

foreach (Control c in ctl)
{
if (c is UserControl & c.ID != "ctl1")
{

TreeView temp = (TreeView)c.Controls[0];
if (temp != null)
temp.Nodes.Add(new TreeNode("Treeview Parent 2"));
}

}
 
Share this answer
 
Comments
Tommy N 30-Jul-15 12:52pm    
So here is what i have so far, instead of using UCs, I'm using <iframe>. When I click on the node on the left, it refresh and populate the tree on the left instead of the right. The content is populated correctly, but it updates the tree on the left instead of the right. How can I make it so it update the right tree? Thanks!

main.html file, i have
...
<body style="height:100%">
<iframe class="lfCtrl" src="lftCtrl.aspx"></iframe>
<iframe class="rtCtrl" src="rtCtrl.aspx"></iframe>
</body>


lfCtrl.aspx file
...
<body style="background-color:#cccccc">
<form id="lfCtrlfrm" name="lfCtrlfrm" runat="server">
<asp:TreeView ID="TreeView1" runat="server">
<nodes>
<asp:TreeNode Text="My Tree" Value="My Tree">

<asp:TreeNode Text="Node A" Value="Node A">
<asp:TreeNode Text="Node A1" Value="Node A1">
<asp:TreeNode Text="Node A2" Value="Node A2">

<asp:TreeNode Text="Node B" Value="Node B">


</form>
</body>

lfCtrl Codebehind
...
Protected Sub TreeView1_SelectedNodeChanged(sender As Object, e As EventArgs) Handles TreeView1.SelectedNodeChanged
Dim strSelectedNode As String

strSelectedNode = TreeView1.SelectedNode.Value

If (strSelectedNode = "Node A1") Then
Response.Redirect("rtCtrl.aspx?Node=A1")
ElseIf (strSelectedNode = "Node A2") Then
Response.Redirect("rtCtrl.aspx?Node=A2")
End If
End Sub


rtCtrl.aspx
...
<body style="background-color:#cccccc">
<form id="rtCtrlfrm" name="rtCtrlfrm" runat="server">
<asp:ScriptManager ID="smRtCtrl" runat="server">
<asp:UpdatePanel ID="upRtCtrl" runat="server">
<contenttemplate>
<asp:TreeView ID="tvwRtCtrl" runat="server">




</form>
</body>

rtCtrl Codebehind
...
Dim docXML As XmlDocument

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strNode As String

docXML = New XmlDocument

strNode= Request.QueryString("Node")

docXML.Load("~/MyXML.xml")
PopulateParentNodes(strNode)
End Sub

Sub PopulateParentNodes(ByVal strNode As String)
tvwRtCtrl.Nodes.Clear()

tvwRtCtrl.Nodes.Add(New TreeNode("Right Nodes")
Dim parentNode As TreeNode = New TreeNode


If (strNode = "A1") Then
Dim nodeList As XmlNodeList = docXML.SelectNodes("/MyNode[@name="A1"]")

For Each xmlNode As XmlNode In nodeList

Dim nodeName As String

nodeName = xmlNode.Attributes("name").Value
parentNode.Text = nodeName
tvwRtCtrl.Nodes.Add(parentNode)
PopulateChildNodes(xmlNode, parentNode)

Next
ElseIf (strNode = "A2") Then
Dim nodeList As XmlNodeList = docXML.SelectNodes("/MyNode[@name="A2"]")

For Each xmlNode As XmlNode In nodeList

Dim nodeName As String

nodeName = xmlNode.Attributes("name").Value
parentNode.Text = nodeName
tvwRtCtrl.Nodes.Add(parentNode)
PopulateChildNodes(xmlNode, parentNode)

Next

End If

End Sub

Sub PopulateChildNodes(ByVal oldXMLNode As XmlNode, ByVal parentNode As TreeNode)
Dim childNodeList As XmlNodeList = oldXMLNode.ChildNodes
Dim strName As String
Dim childNode As TreeNode

For Each xmlNode As XmlNode In childNodeList
childNode = New TreeNode
strName = xmlNode.Attributes("name").Value
childNode.Text = strName
SundararamanS 31-Jul-15 6:32am    
This way will not provide you any solution as what you are trying to do is redirect the response to rtCtrl.aspx.
Ideally which means in your lfCtrl iframe itself you are loading response from rtCtrl.aspx.
The immediate solution that I can think of is by means of finding the second tree control during post back and build your tree accordingly(sample code shared above)

Please check the following about response.redirect method
https://msdn.microsoft.com/en-us/library/ms524309%28v=vs.90%29.aspx[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900