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

Add node at zero index in RadComboBoxTree

3 Answers 96 Views
DropDownTree
This is a migrated thread and some comments may be shown as answers.
Vishram
Top achievements
Rank 1
Vishram asked on 12 May 2014, 10:19 AM
Hi, I tried to add a node at zero index with code :
   protected void fillComboTree(ref  Telerik.Web.UI.RadDropDownTree  cboTree)
    {
                            RadTreeNode radNodeTree= new RadTreeNode();
                            radNodeTree.Value = "S";
                            radNodeTree.Attributes.Add("isHeader","1");
                            radNodeTree.CssClass = "headerStyle";
                            radNodeTree.Text = ResourceHandler.GetValue("CompanyDataSite", m_sCulture);
                            radNodeTree.Font.Size = FontUnit.Small;
                            cboTree.Entries.Add(0,radNodeTree);
  } 

 Please tell me, how to add this object(radNodeTree) to Zero th positon in RadComboBoxTree.

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 14 May 2014, 12:21 PM
Hello,

In order to add a new RadDropDownTree node, you may use the RadDropDownTree.EmbeddedTree property, which gives you access to the control's tree.

Once you have access to the tree, you can create RadTreeNodes and add them to the RadDropDownTree:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        RadDropDownTree1.EmbeddedTree.Nodes.Add(new RadTreeNode() { Text = "foo", Value = "A" });
    }
}

If you would like to create the RadTreeNode in a separate method and add it to the RadDropDownTree, you may follow the same logic:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        RadDropDownTree1.EmbeddedTree.Nodes.Add(fillRadDropDownTree());
    }
}
 
protected RadTreeNode fillRadDropDownTree()
{
    RadTreeNode radNodeTree = new RadTreeNode();
    radNodeTree.Value = "S";
    radNodeTree.Attributes.Add("isHeader", "1");
    radNodeTree.CssClass = "headerStyle";
    radNodeTree.Text = "Test";
    radNodeTree.Font.Size = FontUnit.Small;
 
    return radNodeTree;
}

You may refer to the Working with Nodes in Server-Side Code RadTreeView help article and the RadDropDownTree Data Binding help articles for additional information and examples in code.

Regards,
Dimitar
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Hugo Augusto
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 30 May 2025, 04:24 PM
You didn't answer the question properly. How to insert that node in index 0 or at the top position? This would be very useful to be able to use a default node that replaces the default message. That way it would be possible to know when a no option is selected, since you never implemented a server event for clear selection :(
0
Rumen
Telerik team
answered on 02 Jun 2025, 12:12 PM

Hi Hugo,

Yes, the original answer showed how to add a node, but not specifically how to insert it at index 0. To achieve that, you can directly access the EmbeddedTree property of the RadDropDownTree and use the Insert method on the Nodes collection, like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDropDownTree(RadDropDownTree1);
        }
    }

    private void FillDropDownTree(RadDropDownTree dropDownTree)
    {
        RadTreeView tree = dropDownTree.EmbeddedTree;

        // Create the custom header/default node
        RadTreeNode headerNode = new RadTreeNode
        {
            Text = "Select a company site...",
            Value = "Header",
            CssClass = "headerStyle"
        };
        headerNode.Attributes.Add("isHeader", "1");
        headerNode.Font.Size = FontUnit.Small;

        // Insert at index 0
        tree.Nodes.Insert(0, headerNode);

        // Add dummy data nodes
        tree.Nodes.Add(new RadTreeNode("Site A", "A"));
        tree.Nodes.Add(new RadTreeNode("Site B", "B"));
        tree.Nodes.Add(new RadTreeNode("Site C", "C"));
    }

        <style>
            .headerStyle {
                font-weight: bold;
                color: darkblue;
            }
        </style>
        <telerik:RadDropDownTree ID="RadDropDownTree1" runat="server"
            DefaultMessage="Select an option">
        </telerik:RadDropDownTree>

This will place the node at the top of the dropdown tree and can serve as a default or placeholder option. You can then check for this value on the server side to detect a "no selection" state.

Regards,
Rumen
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
Tags
DropDownTree
Asked by
Vishram
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Hugo Augusto
Top achievements
Rank 2
Iron
Veteran
Iron
Rumen
Telerik team
Share this question
or