Telerik Forums
UI for ASP.NET AJAX Forum
0 answers
3 views

Hi,

I need to restore the expanded state of a RadTreeList after a redirect. I am storing the expanded items in Session before the redirect. However, server-side expansion only expands the root, not the child items.

Can someone share a JavaScript example or approach to fully expand all previously expanded nodes after a redirect using the stored session data? Or maybe provide a better example on Server Side

Thanks!


This is the example I was using on server side, but it only expands the root element


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Telerik.Web.UI;

namespace Momentum.Mapp.WebApp.SysMgmt.Groups
{
    public partial class GroupMaintenanceServer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadTreeList1.NeedDataSource += RadTreeList1_NeedDataSource;
            }
        }

        protected void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            RadTreeList1.DataSource = GetData();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            var expandedItems = RadTreeList1.Items
                .Where(i => i.Expanded)
                .Select(i => (int)i.GetDataKeyValue("ID"))
                .ToList();

            Session["ExpandedNodes"] = expandedItems;
        }

        protected void RadTreeList1_DataBound(object sender, EventArgs e)
        {
            if (Session["ExpandedNodes"] is List<int> expanded)
            {
                foreach (TreeListDataItem item in RadTreeList1.Items)
                {
                    int id = (int)item.GetDataKeyValue("ID");
                    if (expanded.Contains(id))
                        item.Expanded = true;
                }
            }
        }

        protected void RadTreeList1_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == "EditItem" && e.Item is TreeListDataItem dataItem)
            {
                int id = (int)dataItem.GetDataKeyValue("ID");
                Response.Redirect($"EditPage.aspx?id={id}");
            }
        }

        // Ejemplo de datos simples
        private DataTable GetData()
        {
            var dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("ParentID", typeof(int));
            dt.Columns.Add("Name", typeof(string));

            dt.Rows.Add(1, DBNull.Value, "Continents");
            dt.Rows.Add(2, 1, "Asia");
            dt.Rows.Add(3, 1, "Europe");
            dt.Rows.Add(4, DBNull.Value, "Oceans");
            dt.Rows.Add(5, 4, "Pacific");
            dt.Rows.Add(6, 4, "Atlantic");

            return dt;
        }

     }
}

Diego
Top achievements
Rank 1
 updated question on 24 Oct 2025
0 answers
3 views

Hi,

I need to restore the expanded state of a RadTreeList after a redirect. I am storing the expanded items in Session before the redirect. However, server-side expansion only expands the root, not the child items.

Can someone share a JavaScript example or approach to fully expand all previously expanded nodes by the user after a redirect using the stored session data?

If your approach works in Server Side, I will use it on my solution

Thanks team!

This is an example using server side, but it expands only the first root element (not sure why)

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Telerik.Web.UI;

namespace SysMgmt.Groups
{
    public partial class GroupMaintenanceServer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadTreeList1.NeedDataSource += RadTreeList1_NeedDataSource;
            }
        }

        protected void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            RadTreeList1.DataSource = GetData();
        }

        // ✅ Guardar los nodos expandidos antes del render
        protected void Page_PreRender(object sender, EventArgs e)
        {
            var expandedItems = RadTreeList1.Items
                .Where(i => i.Expanded)
                .Select(i => (int)i.GetDataKeyValue("ID"))
                .ToList();

            Session["ExpandedNodes"] = expandedItems;
        }

        protected void RadTreeList1_DataBound(object sender, EventArgs e)
        {
            if (Session["ExpandedNodes"] is List<int> expanded)
            {
                foreach (TreeListDataItem item in RadTreeList1.Items)
                {
                    int id = (int)item.GetDataKeyValue("ID");
                    if (expanded.Contains(id))
                        item.Expanded = true;
                }
            }
        }

        protected void RadTreeList1_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == "EditItem" && e.Item is TreeListDataItem dataItem)
            {
                int id = (int)dataItem.GetDataKeyValue("ID");
                Response.Redirect($"EditPage.aspx?id={id}");
            }
        }

       
        private DataTable GetData()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("ParentID", typeof(int));
            table.Columns.Add("Text", typeof(string));
            table.Columns.Add("Value", typeof(string));

            // Root nodes
            table.Rows.Add(1, DBNull.Value, "Root 1", "A");
            table.Rows.Add(2, DBNull.Value, "Root 2", "B");

            // Children of Root 1
            table.Rows.Add(3, 1, "Child 1.1", "A1");
            table.Rows.Add(4, 1, "Child 1.2", "A2");

            // Children of Root 2
            table.Rows.Add(5, 2, "Child 2.1", "B1");
            table.Rows.Add(6, 2, "Child 2.2", "B2");

            // Sub-child
            table.Rows.Add(7, 3, "SubChild 1.1.1", "A1a");

            return table;
        }
    }
}
Diego
Top achievements
Rank 1
 asked on 24 Oct 2025
1 answer
66 views

Good morning! I would like to know if it is possible to save the state of the radtreelist with the nodes that were opened or closed and this way when the user reloads the screen it will be displayed the way it was before?

Radtreelist asp.net c#

Vasko
Telerik team
 answered on 31 Oct 2024
0 answers
58 views
I have a RadTreeList and my data consists of 3 levels/hierarchy. I want to export it to excel with the grouping functionality. Unfortunately, Radtreelist doesn't have this functionality. Has anyone done something similar with RadSpreadProcessing?

I've read the documentation https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/grouping but haven't had any luck in implementing it.
Melville
Top achievements
Rank 1
 asked on 12 Aug 2024
1 answer
138 views

Hi,

I'm trying to use the Treelist for a data source with the following definition

public class MyGridItem
{
	public string ID { get; set; }
	public string Name { get; set; }
	public string ParentID { get; set; }
	public bool IsIncluded { get; set; }
	public bool IsExcluded { get; set; }
}

In markup I've got


<telerik:RadTreeList ID="RadTreeMarkers" runat="server" ParentDataKeyNames="ParentID" DataKeyNames="ID" AutoGenerateColumns="false"
	OnNeedDataSource="RadTreeMarkers_NeedDataSource" AllowMultiItemEdit="true" ExpandCollapseMode="Client" EditMode="InPlace"
	AllowPaging="false" AllowSorting="false" OnItemDataBound="RadTreeMarkers_ItemDataBound">
	<Columns>
		<telerik:TreeListBoundColumn DataField="ID" UniqueName="ID" Visible="false" ForceExtractValue="Always"></telerik:TreeListBoundColumn>
		<telerik:TreeListBoundColumn DataField="ParentID" UniqueName="ParentID" Visible="false"></telerik:TreeListBoundColumn>
		<telerik:TreeListBoundColumn DataField="Name" UniqueName="Name" HeaderText="Mark" ReadOnly="true"></telerik:TreeListBoundColumn>
		<telerik:TreeListCheckBoxColumn DataField="IsIncluded" UniqueName="IsIncluded" HeaderText="Included" ForceExtractValue="Always"></telerik:TreeListCheckBoxColumn>
		<telerik:TreeListCheckBoxColumn DataField="IsExcluded" UniqueName="IsExcluded" HeaderText="Excluded" ForceExtractValue="Always"></telerik:TreeListCheckBoxColumn>
	</Columns>
</telerik:RadTreeList>

And then in the code behind I've got
protected void RadTreeMarkers_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
{
	var treeData = MyFacade.GetTreeData();
	var treeDataGridItems = treeData.Select(m => new GridItem
	{
		ID = m.ID.ToString(),
		Name = m.Name,
		ParentID = m.ParentID.HasValue ? m.ParentID.Value.ToString() : string.Empty,
		IsIncluded = m.IsIncluded.HasValue && m.IsIncluded == true,
		IsExcluded = m.IsIncluded.HasValue && m.IsIncluded == false
	}
	).ToList();
	RadTreeMarkers.DataSource = treeDataGridItems;
}

protected void RadTreeMarkers_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
{
	if (e.Item is TreeListDataItem item)
	{
		item.Edit = true;
	}
}

The intention is for (only) the two checkboxes to be always editable in the client.

However, when rendered in the client the root items are editable but none of the child items is editable.

What am I doing wrong?

Doncho
Telerik team
 answered on 05 Apr 2023
1 answer
133 views
I have a RadTreeList that is bound to a very large amount of data (100,000 + rows of data). The user is searching and is only interested in only part of the data, perhaps 20-30 rows anywhere in the data bound to the control. These rows will most likely be between child/parents within the treelist and wont have a nice parent/child relationship to display properly. What I want is to only be able to display a partial view of the data around what the user searched on and have the data look like a "snapshot" of the entire treelist. Has anyone done anything like this or are there any suggestions? Any help is appreciated.
Peter Milchev
Telerik team
 answered on 07 Jun 2022
1 answer
152 views

Hello,

I am attempting to integrate the drag/drop feature into a RadTreeList. The dragging is enabled and seems to work great but when an item is dropped it attempts a postback but does not change position. I assume this is because I need to manually assign it a value in comparison to a parent item. Does anyone have any experience or examples they could provide to display this functionality better? The most I have found online was this brief article:

https://docs.telerik.com/devtools/aspnet-ajax/controls/treelist/items/items-drag-and-drop

My code:

       <telerik:RadTreeList ID="treeViewGrid" runat="server" DataKeyNames="ModuleId" CommandItemDisplay="Top" ParentDataKeyNames="ParentId" AutoGenerateColumns="false" EnableDragAndDrop="true">
            <ClientSettings AllowItemsDragDrop="true">
    <Selecting AllowItemSelection="True" />
                </ClientSettings>
            <Columns>

....

</columns>

....

 

Database:

moduleid
1
displayorder
1
moduletype
1
parentid
0
displayorder
2
moduletype
1
parentid
0
moduleid
2

 

 

My guess would be that there is a OnDropItem that runs server side where I can assign a new display order based on the previous order. Any help on this is appreciated! Thanks!

Eric R | Senior Technical Support Engineer
Telerik team
 answered on 13 Oct 2021
2 answers
171 views

 

Once we add a selection column to the radtreelist,  it left aligns the next column.   However we need the 2nd column to align with the checkbox, is OK for all other columns to align.   Thus for  your demo page, the behavior we need is the Bound Column to have "Appetizers" immediately after the check so it appears as a label for the checkbox.  How would I achieve this?

 

Van
Top achievements
Rank 1
Iron
Iron
 answered on 03 Aug 2021
1 answer
145 views

In Brief

The OnItemCommand ExpandCollapse event command doesn't fire when ClientSettings.AllowPostBackOnItemClick="true"

Detail

I am using a TreeList to display a company hierarchy. It is defined as follows.

<telerik:RadTreeList
    ID="lstCompanyTree"
    runat="server"
    PageSize="12"    
    AllowLoadOnDemand="false"    
    AllowPaging="true"
    Width="300px"     
    OnNeedDataSource="lstCompanyTree_NeedDataSource"
    OnItemCommand="lstCompanyTree_ItemCommand"
    OnDataBound="lstCompanyTree_DataBound"
    OnItemDataBound="lstCompanyTree_ItemDataBound"
    OnPageIndexChanged="lstCompanyTree_PageIndexChanged"
    OnPreRender="lstCompanyTree_PreRender"     
    AutoGenerateColumns="false"          
    AllowSorting="false"     
    CssClass="CompanyListTree"          
    ShowTreeLines="true"   
    ExpandCollapseMode="Server"
    DataKeyNames="CompanyID"
    ParentDataKeyNames="ParentCompanyID"
    ClientDataKeyNames="CompanyID,ObjectID"
    >
    <ClientSettings AllowPostBackOnItemClick="true" >
        <Selecting AllowItemSelection="true" />                      
    </ClientSettings>
    <AlternatingItemStyle BackColor="Transparent" />
    <ItemStyle BackColor="Transparent" />   
    <Columns>       
        <telerik:TreeListTemplateColumn >
            <ItemTemplate>
                <div class="CompanyListItem">
                    <div class="layoutRow" <%# Eval("Style") %>>
                        <div class="layoutCell dataCellMin200"><strong><%# Eval("Name") %></strong></div>
                    </div>     
                    <div class="layoutRow">
                        <div class="layoutCell dataCellMin200"><%# Eval("SubHeading")%></div>
                    </div>           
                </div>
            </ItemTemplate>           
        </telerik:TreeListTemplateColumn>
    </Columns>               
</telerik:RadTreeList>

 

I am trying to implement code when the Expand collapse button is clicked, and load the details of the company when an item is selected.

The problem is in the OnItemCommand event.

The ItemClick event command is overriding the ExpandCollapse command. If you disable AllowPostBackOnItemClick, then you get the ExpandCollapse command as expected.

that is, in this code block

protected void lstCompanyTree_ItemCommand(object sender, Telerik.Web.UI.TreeListCommandEventArgs e)
{
..
}

 

e.CommandName is always "ItemClick" when AllowPostBackOnItemClick is true.

if you disable AllowPostBackOnItemClick , then e.CommandName = "ExpandCollapse" when you click the ExpandCollapse button. I haven't been able to test the value when the item is clicked as the postback is disabled, but one would assume e.CommandName = "ItemClick" in this case.

 

 

Doncho
Telerik team
 answered on 18 Aug 2020
4 answers
167 views
I am trying to insert/edit items in the TreeList using a pop-up usercontrol.And when finish insert/update, user control can not close .Please tell me how to close Popup user control.Thank u .

Here is my code in User control :

<telerik:RadButton ID="btnInsert" runat="server" Text="Insert"   Width="75px"
                CommandName ="PerformInsert"
                Visible ="<%#DataItem is Telerik.Web.UI.TreeListInsertionObject %>"
                onclick="btnInsert_Click">
                <Icon PrimaryIconCssClass ="rbSave" />
        </telerik:RadButton>
        <telerik:RadButton ID="btnSave" runat="server" Text="Save"   Width="75px"
                CommandName ="Update"
                Visible ="<%#!(DataItem is Telerik.Web.UI.TreeListInsertionObject) %>"
                onclick="btnSave_Click">
              <Icon PrimaryIconCssClass ="rbSave" />
        </telerik:RadButton>
        <telerik:RadButton ID="btnCancel" runat="server" Text="Cancel"  Width="75px"
                CommandName ="Cancel"  CausesValidation="False">
                 <Icon PrimaryIconCssClass ="rbCancel" />
</telerik:RadButton>

Oscar
Top achievements
Rank 1
 answered on 28 May 2020
Narrow your results
Selected tags
Tags
+? more
Top users last month
Ambisoft
Top achievements
Rank 2
Iron
Pascal
Top achievements
Rank 2
Iron
Matthew
Top achievements
Rank 1
Sergii
Top achievements
Rank 1
Iron
Iron
Andrey
Top achievements
Rank 1
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Ambisoft
Top achievements
Rank 2
Iron
Pascal
Top achievements
Rank 2
Iron
Matthew
Top achievements
Rank 1
Sergii
Top achievements
Rank 1
Iron
Iron
Andrey
Top achievements
Rank 1
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?