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

5 posts, 0 answers
  1. SSirica
    SSirica avatar
    116 posts
    Member since:
    Jan 2007

    Posted 28 May 2020 Link to this post

    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.

  2. SSirica
    SSirica avatar
    116 posts
    Member since:
    Jan 2007

    Posted 28 May 2020 Link to this post

    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.  

  3. SSirica
    SSirica avatar
    116 posts
    Member since:
    Jan 2007

    Posted 29 May 2020 Link to this post

    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!

  4. Peter Milchev
    Admin
    Peter Milchev avatar
    898 posts

    Posted 03 Jun 2020 Link to this post

    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.
  5. SSirica
    SSirica avatar
    116 posts
    Member since:
    Jan 2007

    Posted 03 Jun 2020 in reply to Peter Milchev Link to this post

    Thanks, but I ended up going in a totally different direction.  I used the templates and it's working now.  
Back to Top