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

how to hide expanding icon (+ sign) with nodes with no child

2 Answers 418 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
AratiS
Top achievements
Rank 1
AratiS asked on 28 Mar 2013, 08:23 PM
Hi,

I am using RadTreeView with server side load on demand. The issue I am having is I don't want the '+' sign to be displayed for a node if it doesn't have a child node. How can I achieve this with server side load on demand. Below is the code I am using to create root and child nodes.

 ASPX code:

<telerik:RadTreeView runat="server" ID="RadTreeView1" OnNodeExpand="RadTreeView1_NodeExpand" >
                 <ContextMenus>
                <telerik:RadTreeViewContextMenu ID="CM" runat="server">
                <Items>
                <telerik:RadMenuItem Value="CreateSibling" Text="Create Sibling">
                </telerik:RadMenuItem>
                <telerik:RadMenuItem Value="CreateChild" Text="Create Child">
                </telerik:RadMenuItem>
                </Items>
                </telerik:RadTreeViewContextMenu>
                <telerik:RadTreeViewContextMenu ID="EmptyCM" runat="server">
                </telerik:RadTreeViewContextMenu>
                </ContextMenus>
    </telerik:RadTreeView>

c# code:

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSide);
                }
                
            }
            catch (Exception ex)
            {
                RadAjaxManager1.Alert(ex.ToString());
            }
        }

private static void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            DataTable data = GetData(new SqlCommand("select Description,ID from Table1"));

            foreach (DataRow row in data.Rows)
            {
                RadTreeNode rootnode = new RadTreeNode();
                rootnode.Text = row["Description"].ToString();
                rootnode.Value = row["ID"].ToString();
                rootnode.ContextMenuID = "CM";
                rootnode.ExpandMode = expandMode;
                treeView.Nodes.Add(rootnode);
            }
        }

 protected void RadTreeView1_NodeExpand(object sender,RadTreeNodeEventArgs e)
        {
            if (e.Node.Nodes.Count == 0)
                PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSide);
        }

        private static void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            DataTable data = GetChildNodes(e.Node.Value,e.Node.ContextMenuID);

            foreach (DataRow row in data.Rows)
            {
                RadTreeNode childnode = new RadTreeNode();
                childnode.Text = row["Description"].ToString();
                childnode.Value = row["ID"].ToString();
                if (!(childnode.Value == e.Node.Value))
                {
                    e.Node.Nodes.Add(childnode);
                    childnode.ExpandMode = expandMode;
                }

                childnode.ContextMenuID = "CM";
                
            }
            if (e.Node.Nodes.Count > 0)
            {
                e.Node.Expanded = true;
            }
            else
            {
                
               e.Node.ExpandMode = TreeNodeExpandMode.ClientSide;
                //e.Node.Expanded = false;
            }
        }

2 Answers, 1 is accepted

Sort by
0
Accepted
Nencho
Telerik team
answered on 02 Apr 2013, 01:18 PM
Hello Arati,

Since you had implemented the Load On Demand functionality, you could not be aware if there are any child node for a certain node, before it is expanded. Therefor, the desired functionality could not be achieved initially, since a request to the DataBase should be fired to extract the child nodes, if any exist.

Regards,
Nencho
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Bob
Top achievements
Rank 1
answered on 06 May 2013, 11:42 PM
In order to achieve the desired results, you have to return a column from your sql query that indicates if the record has any children. You can then use this value to determine whether or not to set ExpandMode.
Not knowing what your table structure looks like, I'm just providing a sample query to return the number of children for a given record assuming some sort of ParentID is related to the IDs in the table.

select
    t.Description,
    t.ID,
    (select count(*) from Table1 where ParentID = t.ID) as Children
from
    Table1 t
where
    t.ParentID = @ID

//Then in your code when you build the tree, check to see if the node
//has any Children. If it does then set the ExpandMode
//If it doesn't then don't set ExpandMode
if ( row["Children"] != "0" )
{
    childNode.ExpandMode = expandMode;
}
Tags
TreeView
Asked by
AratiS
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Bob
Top achievements
Rank 1
Share this question
or