RadDropDownTree with checkboxes created and checked dynamically

3 Answers 54 Views
CheckBox DropDownTree
Emmanuel
Top achievements
Rank 1
Iron
Emmanuel asked on 16 Mar 2022, 01:23 PM

Hello,

We are using you RadDropDownTree component.

We create it dynamically with this code :

RadDropDownTree field = new RadDropDownTree();
field.Enabled = !(param.State == ParameterStateEnum.HasOutstandingDependencies);
field.CheckBoxes = DropDownTreeCheckBoxes.SingleCheck;
field.DefaultMessage = "Sélectionner parmi ...";
field.ID = param.Name;
field.Width = Unit.Pixel(250);

field.EnableFiltering = true;
field.FilterSettings.Highlight = DropDownTreeHighlight.Matches;
field.FilterSettings.EmptyMessage = "Taper une recherche...";
field.FilterSettings.MinFilterLength = 3;

field.DropDownSettings.AutoWidth = DropDownTreeAutoWidth.Enabled;
field.ButtonSettings.ShowCheckAll = true;
field.ButtonSettings.ShowClear = true;

if (param.ValidValues != null)
{
  foreach (ValidValue val in param.ValidValues)
  {
     RadTreeNode item = new RadTreeNode();
     item.Value = val.Value;
     item.Text = val.Label;
     item.Checked = defaultValues.ContainsKey(val.Value);
     field.EmbeddedTree.Nodes.Add(item);
  }
}

The problem is that when we checked an item by code, the item is not displayed in the entries display zone.

If we check them while running with the mouse, it is well displayed.

 

Can you please help me with this problem ?

 

Thank you.

3 Answers, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 18 Mar 2022, 12:59 PM

Hi Emmanuel,

I am afraid I was not able to reproduce the described behavior on my side.

Here is a sample I used to test the behavior:

ASPX

<asp:PlaceHolder runat="server" ID="PlaceHolder1" />

C#

protected void Page_Init(object sender, EventArgs e)
{
    var ddt = GenerateDropDownTree();
    PlaceHolder1.Controls.Add(ddt);
}
protected RadDropDownTree GenerateDropDownTree() { RadDropDownTree field = new RadDropDownTree(); field.Enabled = true; field.CheckBoxes = DropDownTreeCheckBoxes.SingleCheck; field.DefaultMessage = "Sélectionner parmi ..."; field.ID = "RadDropDownTree1"; field.Width = Unit.Pixel(250); field.EnableFiltering = true; field.FilterSettings.Highlight = DropDownTreeHighlight.Matches; field.FilterSettings.EmptyMessage = "Taper une recherche..."; field.FilterSettings.MinFilterLength = 3; field.DropDownSettings.AutoWidth = DropDownTreeAutoWidth.Enabled; field.ButtonSettings.ShowCheckAll = true; field.ButtonSettings.ShowClear = true; for (int i = 0; i < 10; i++) { RadTreeNode item = new RadTreeNode(); item.Value = i.ToString(); item.Text = "Name " + i; item.Checked = i % 2 == 0; field.EmbeddedTree.Nodes.Add(item); } return field; }

Could you please modify the code so it shows us the issue and send it back to us for troubleshooting?

Looking forward to your reply.

Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Emmanuel
Top achievements
Rank 1
Iron
answered on 18 Mar 2022, 01:11 PM

Hello,

Thank you very much for your reply.

The code you provide is the right example. When I execute it, I have this :

Now, I check with the mouse another item. I have this :

As you can see, the defaultmessage "Selectionner parmi ... " is replaced by "Name 1".

Why is'nt it "Name 0, Name 1, Name 2, Name 4, Name 6, Name 8" ?

I would like to have "Name 0, Name 2, Name 4, Name 6, Name 8" at this first execution.

 

Is it way to do it ?

 

Tahnk you for your help.

 

0
Doncho
Telerik team
answered on 18 Mar 2022, 01:52 PM

Hi Emmanuel,

Thank you for the clarification!

It helped me understand a crucial mistake in the approach. Creating instances of Nodes might be appropriate for the TreeView but for the RadDropDownTree, you will need to bind data to it in order to create and populate its data.

I would suggest you check out the following article - How to Create An Entry or Select a Node.

I have modified the approach to use a standard binding procedure and the CreateEntry() method to check/select the desired nodes. Here is how it looks now:

RadDropDownTree ddt;
protected void Page_Init(object sender, EventArgs e)
{
    ddt = GenerateDropDownTree();
    PlaceHolder1.Controls.Add(ddt);
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddt.DataBind();
    }
}
protected RadDropDownTree GenerateDropDownTree()
{
    RadDropDownTree field = new RadDropDownTree();
    field.Enabled = true;
    field.CheckBoxes = DropDownTreeCheckBoxes.SingleCheck;
    field.DefaultMessage = "Sélectionner parmi ...";
    field.ID = "RadDropDownTree1";
    field.Width = Unit.Pixel(250);

    field.EnableFiltering = true;
    field.FilterSettings.Highlight = DropDownTreeHighlight.Matches;
    field.FilterSettings.EmptyMessage = "Taper une recherche...";
    field.FilterSettings.MinFilterLength = 3;

    field.DropDownSettings.AutoWidth = DropDownTreeAutoWidth.Enabled;
    field.ButtonSettings.ShowCheckAll = true;
    field.ButtonSettings.ShowClear = true;
    field.DataTextField = "Label";
    field.DataValueField = "Value";
    field.DataSource = GetData();
    field.NodeDataBound += Field_NodeDataBound;

    return field;
}

private void Field_NodeDataBound(object sender, DropDownTreeNodeDataBoundEventArguments e)
{
    var node = e.DropDownTreeNode;
    if (int.Parse(node.Value) % 2 == 0)
    {
        node.CreateEntry();
    }
}

protected List<DummyObject> GetData()
{
    var list = new List<DummyObject>();

    for (int i = 0; i < 10; i++)
    {
        list.Add(new DummyObject()
        {
            Value = i.ToString(),
            Label = "Name " + i
        });
    }

    return list;
}

public class DummyObject
{
    public string Value { get; set; }
    public string Label { get; set; }
}

I hope this helps.

Kind regards,
Doncho
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Emmanuel
Top achievements
Rank 1
Iron
commented on 18 Mar 2022, 03:44 PM

Hello again,

Thank you for your reply.

I tried this out and it is working fine.

 

A question :

I try to do with this, instead of NodeDataBound event :

            foreach (RadTreeNode node in field.EmbeddedTree.Nodes)
            {
                if (int.Parse(node.Value) % 2 == 0)
                    ((DropDownTreeNode)node).CreateEntry();
            }

It is not possible to convert RadTreeNode in DropDownTreeNode...

Is there any way to get the DropDownTreeNode object in this loop ?

 

Thank you.

Doncho
Telerik team
commented on 22 Mar 2022, 12:43 PM

Hi Emmanuel,

I am afraid you cannot reach the DropDownTreeNode from the corresponding RadTreeNode. Yet, there is an option to create the entries by interacting with the RadTreeNodes only.

You can set the Checked property of the desired RadTreeNode(s) to 'true' and apply the changes to the DropDwonTree by calling the SyncEmbeddedTree() function as suggested in the Accessing the Embedded Tree article:

foreach (RadTreeNode node in field.EmbeddedTree.Nodes)
{
    if (int.Parse(node.Value) % 2 == 0)
        node.Checked = true;
}
field.SyncEmbeddedTree();

 

Emmanuel
Top achievements
Rank 1
Iron
commented on 22 Mar 2022, 09:35 PM

Hello,

 

Thank you very much for your help.

It is exactly what I needed.

 

Sincerely!

Tags
CheckBox DropDownTree
Asked by
Emmanuel
Top achievements
Rank 1
Iron
Answers by
Doncho
Telerik team
Emmanuel
Top achievements
Rank 1
Iron
Share this question
or