New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Programmatic Definition

RadTreeList created entirely in code-behind on Page_Init

EmployeeIDLast NameFirst NameHire DateCityPostal CodeCountryReports To
2FullerAndrew14/08/1992Tacoma98401USA 
  1DavolioNancy01/05/1992Seattle98122USA2
  3LeverlingJanet01/04/1992Kirkland98033USA2
  4PeacockMargaret03/05/1993Redmond98052USA2
 5BuchananSteven17/10/1993LondonSW1 8JRUK2
  8CallahanLaura05/03/1994Seattle98105USA2



RadTreeList decalared statically with structure defined on Page_Load

EmployeeIDLast NameFirst NameHire DateCityPostal CodeCountryReports To
2FullerAndrew14/08/1992Tacoma98401USA 
  1DavolioNancy01/05/1992Seattle98122USA2
  3LeverlingJanet01/04/1992Kirkland98033USA2
  4PeacockMargaret03/05/1993Redmond98052USA2
 5BuchananSteven17/10/1993LondonSW1 8JRUK2
  8CallahanLaura05/03/1994Seattle98105USA2

This demo shows the two possible approaches you can use when defining the RadTreeList control structure dynamically:

  • Create the RadTreeList entirely in code-behind on Page_Init.
  • Add the RadTreeList declaration statically in the page mark-up and configure it on Page_Load.

When RadTreeList is created on Page_Init, you should first set the columns' properties and after that add them to the RadTreeList Columns collection.

On the other hand, when the RadTreeList is declared statically and configured on Page_Load, you should first add the columns to the RadTreeList Columns collection and after that set their properties. Also check the condition (Not IsPostBack) to avoid adding the same structure objects to the treelist twice.

  • DefaultCS.aspx
  • DefaultCS.aspx.cs
<%@ Page Language="c#" Inherits="Telerik.TreeListExamplesCSharp.DefiningStructure.ProgrammaticDefinition.DefaultCS"CodeFile="DefaultCS.aspx.cs"  %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadTreeList2">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadTreeList2" LoadingPanelID="RadAjaxLoadingPanel1"></telerik:AjaxUpdatedControl>
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <div class="demo-container" style="max-width:800px">
        <h2 class="qsfSubtitle">RadTreeList created entirely in code-behind on Page_Init</h2>
        <telerik:RadAjaxPanel runat="server" ID="RadAjaxPanelPanel1" LoadingPanelID="RadAjaxLoadingPanel1">
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        </telerik:RadAjaxPanel>
        <br />
        <br />
        <br />
        <h2 class="qsfSubtitle">RadTreeList decalared statically with structure defined on Page_Load</h2>
        <telerik:RadTreeList RenderMode="Lightweight" ID="RadTreeList2" runat="server">
        </telerik:RadTreeList>
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [ReportsTo], [EmployeeID], [LastName], [FirstName], [HireDate], [City], [PostalCode], [Country] FROM [Employees]"></asp:SqlDataSource>
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server"></telerik:RadAjaxLoadingPanel>
    </form>
</body>
</html>

Support & Learning Resources

Find Assistance