Template on root level only

2 posts, 0 answers
  1. Stephen
    Stephen avatar
    8 posts
    Member since:
    Feb 2011

    Posted 10 Aug 2011 Link to this post

    Here is my situation. I have a tree that uses load on demand. The first level is Offices and the second level are Contacts in corresponding offices.
    Now, the data source for the offices also returns the number of contacts in the office, which is helpful for this load on demand scenario (the user now knows if the office has nodes in it). However, to display the number in a different color beside the office title, I need to use a template of the Office Nodes. But I want the Contact Nodes below to be regular RadTreeNodes. Now I got it working one way programmatically, but I feel using template on the ASPX side would faster.

    Here is my attempt at the ASPX side, which is completely broken and completely wrong, but I'm using it to give you a sense of where I am going.
    <telerik:RadTreeView ID="_tvOffice" AutoPostBack="true" Width="100%" Skin="Office2007"
                EnableDragAndDropBetweenNodes="true" EnableDragAndDrop="True"
                runat="server"
                DataFieldID="ID" ContextMenuItemClick="_tvOffice_ContextMenuItemClick" OnNodeExpand="_tvOffice_NodeExpand">
               <NodeTemplate>
                    <telerik:RadTreeNode runat="server">
                    </telerik:RadTreeNode>
                </NodeTemplate>
                <Nodes>
                    <telerik:RadTreeNode EnableContextMenu="false" CssClass="spIcon spFldr_C" AllowDrag="false"
                        AllowDrop="true" ExpandMode="ServerSideCallBack" runat="server">
                        <NodeTemplate>
                            <asp:Label ID="_lblTitle" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.officeTitle")%>'></asp:Label>
                            <asp:Label ID="_lblCount" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ContactCount", "({0})")%>'
                                CssClass="lblProjectNum"></asp:Label>
                        </NodeTemplate>
                    </telerik:RadTreeNode>
                </Nodes>
                <DataBindings>
                  <telerik:RadTreeNodeBinding Depth="0" EnableContextMenu="false" CssClass="spIcon spFldr_C" AllowDrag="false"
                        AllowDrop="true" ExpandMode="ServerSideCallBack" TextField="officeTitle" ValueField="ID" CategoryField="Category" />
                </DataBindings>
            </telerik:RadTreeView>

    private void RebindOfficeTree(int? companyId)
            {
                if (companyId == null) return;
                
                var db = new DBStoredProc();
                var officeList = db.Offices_OnDemand(companyId);
                _tvOffice.DataSource = officeList;
                _tvOffice.DataBind();
            }
     
    private static void PopulateContactsByOffice(RadTreeNodeEventArgs e)
            {
                if (e == null || e.Node.Value == null) return;
     
                var db = new DataContext();
                var officeId = int.Parse(e.Node.Value);
                var contactList = Contacts.LoadByOfficeId(db, officeId);
     
                foreach (var contact in contactList)
                {
                    var node = new RadTreeNode
                    {
                        Text = contact.FullName,
                        Value = contact.Id.ToString(),
                        CssClass = "spIcon spContact",
                        Category = "Contact",
                        AllowDrag = true,
                        AllowDrop = false
                    };
                    e.Node.Nodes.Add(node);
                }
                e.Node.Expanded = true;
            }


    Also, I am grabbing the offices by clicking on a Company Node on another tree, but using per-node template, a "blank" tree node appears in the Office tree on page load (before selecting any company).

    The point here is speed. I want to use the fastest, most optimized method to do this.
  2. Nikolay Tsenkov
    Admin
    Nikolay Tsenkov avatar
    734 posts

    Posted 15 Aug 2011 Link to this post

    Hi Stephen,

     Here is an article on adding templates runtime: http://www.telerik.com/help/aspnet-ajax/treeview-templates-runtime.html

    Also you can use the above approach in combination with handling the TemplateNeeded event - here is a help article on that: http://www.telerik.com/help/aspnet-ajax/e_telerik_web_ui_radtreeview_templateneeded.html

    I hope this helps.

    Regards,
    Nikolay Tsenkov
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Back to Top