This is a migrated thread and some comments may be shown as answers.

Node.Controls.Clear() not working for dynamically created/added controls

4 Answers 93 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
SSirica asked on 28 May 2020, 03:20 PM

I have the following treeview

<telerik:RadTreeView ID="tvProcInst" runat="server" CheckBoxes="True" CausesValidation="False"
    DataTextField="LEVEL_NAME" DataValueField="LEVEL_CODE" RenderMode="Lightweight"
    CssClass="Left" MultipleSelect="True">
</telerik:RadTreeView>

 

that is loaded from a linq query once in the page load.  That all works fine.  Each time a user checks a node I add a combobox with the following code

Private Sub tvProcInst_NodeCheck(sender As Object, e As RadTreeNodeEventArgs) Handles tvProcInst.NodeCheck
    Dim cb As New RadComboBox
    Dim cn As New RadTreeNode
 
    If e.Node.ParentNode IsNot Nothing Then
        Return
    End If
 
    If e.Node.HasControls Then
        If Not e.Node.Checked Then
            e.Node.Controls.Clear()
        End If
        Return
    End If
 
    Dim row As New RadComboBoxItem
    row.Value = "R"
    row.Text = "Recurring"
    cb.Items.Add(row)
 
    row = New RadComboBoxItem
    row.Value = "N"
    row.Text = "Non-Recurring"
 
    cb.Items.Add(row)
    cb.ID = "cb" & e.Node.Value
 
    cn.Controls.Add(cb)
    cn.Checkable = False
 
    e.Node.Nodes.Add(cn)
    e.Node.Expanded = True
End Sub

 

it will add the combobox no problem, but will not clear it upon unchecking the checkbox, and when checking another checkbox it will create the new one, but the previously created combobox goes away or goes invisible I can't figure out which because the controls.count is still 1 but yet I can't see it.

Hopefully this makes sense.  If screen shots will help I can supply them if needed.

4 Answers, 1 is accepted

Sort by
0
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
answered on 28 May 2020, 06:53 PM

Never mind I was adding the combobox in the wrong spot.  This ended up working

Private Sub tvProcInst_NodeDataBound(sender As Object, e As RadTreeNodeEventArgs) Handles tvProcInst.NodeDataBound
    Dim cn As RadTreeNode
    cn = New RadTreeNode
 
    cn.Controls.Add(GetCB(e.Node.Value.ToString))
    cn.Checkable = False
 
    e.Node.Nodes.Add(cn)
End Sub

 

Now I have to loop thru the nodes and find the value of the comboboxes.  Wish me luck.  

0
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
answered on 29 May 2020, 02:01 PM

Nope...I must have jinxed myself.  I can't find the dynamically added comboboxes(added using the above code) to save my life.  Here is the code I'm using:

For Each obj As RadTreeNode In tvProcInst.Nodes
    If obj.Checked Then
        Dim cb As Object
        cb = obj.FindControl(obj.UniqueID & "cb" & obj.Value.ToString)
    End If
Next

 

FindControl finds absolutely Nothing!

0
Peter Milchev
Telerik team
answered on 03 Jun 2020, 09:28 AM

Hello SSirica,

Please ensure that the ID you are passing to the Node is the correct one and also the node you are checking has the ComboBox as a child control. You can use the Quick Watch(F9) of Visual Studio to inspect the node once you hit the breakpoint.

Here is the code that gets the ComboBoxes properly:

<telerik:RadButton runat="server" ID="RadButton1" Text="Get Selection" AutoPostBack="true" OnClick="RadButton1_Click" />

<telerik:RadTreeView ID="tvProcInst" runat="server" CheckBoxes="True" CausesValidation="False"
    DataTextField="LEVEL_NAME" DataValueField="LEVEL_CODE" RenderMode="Lightweight"
    CssClass="Left" MultipleSelect="True" OnNodeDataBound="tvProcInst_NodeDataBound">
</telerik:RadTreeView>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            tvProcInst.DataSource = Enumerable.Range(1, 5).[Select](Function(x) New With {Key
                .LEVEL_CODE = x, Key
                .LEVEL_NAME = "Node #" & x
            })
            tvProcInst.DataBind()
        End If
    End Sub

    Public Function GetCB(ByVal id As String) As RadComboBox
        Dim cb = New RadComboBox()
        cb.ID = id & "_ComboBox"
        cb.Items.Add(New RadComboBoxItem() With {
            .Text = "Item 1",
            .Value = "1"
        })
        cb.Items.Add(New RadComboBoxItem() With {
            .Text = "Item 2",
            .Value = "2"
        })
        Return cb
    End Function

    Public Sub tvProcInst_NodeDataBound(ByVal sender As Object, ByVal e As RadTreeNodeEventArgs)
        Dim cn As RadTreeNode
        cn = New RadTreeNode()
        cn.Controls.Add(GetCB(e.Node.Value.ToString()))
        cn.Checkable = False
        e.Node.Nodes.Add(cn)
    End Sub

    Protected Sub RadButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
        For Each node As RadTreeNode In tvProcInst.GetAllNodes().Where(Function(x) x.Checkable = False)
            Dim cb = TryCast(node.FindControl(node.ParentNode.Value & "_ComboBox"), RadComboBox)
        Next
    End Sub

Depending on the GetCB method, you should use the same value that you have used for generating the ComboBox id.

Another thing worth mentioning is that you are passing the Parent node value to the GetCB method while you are trying to find the ComboBox via the unique ID of the combobox container node.

Also, the Nodes collection of the TreeView contains only the root nodes, while the TreeView.GetAllNodes() method returns all nodes in the treeview.

Regards,
Peter Milchev
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
answered on 03 Jun 2020, 01:35 PM
Thanks, but I ended up going in a totally different direction.  I used the templates and it's working now.  
Tags
TreeView
Asked by
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
SSirica
Top achievements
Rank 3
Iron
Iron
Iron
Peter Milchev
Telerik team
Share this question
or