Node Thinks It's Collapsed In Code Behind

3 posts, 0 answers
  1. Casey
    Casey avatar
    14 posts
    Member since:
    May 2011

    Posted 23 Feb 2013 Link to this post


    I'm hoping someone else has run into this problem.  I have a simple page with a treeview and a button.  The button calls code that exports the treeview to a .csv file.  If a node is expanded then I want to export the child nodes as well.  For some reason in code behind it is telling me that all nodes at level 1 are collapsed even though I expanded them before I clicked the export button.  Any ideas?

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeView.aspx.cs" Inherits="DXiCustomerData.TreeView" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <head runat="server">
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            <telerik:RadButton ID="btnExportTree" runat="server" Text="Export Tree" OnClick="btnExportTree_Click"
            <telerik:RadTreeView ID="rtvResults" runat="server" Visible="true">
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using System.IO;
    using System.Collections;
    using Telerik.Web;
    using Telerik.Web.UI;
    namespace DXiCustomerData
        public partial class TreeView : System.Web.UI.Page
            private String glogDate;
            protected void Page_Load(object sender, EventArgs e)
                SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString);
                PopulateTree pt = new PopulateTree();
                pt.AddNodes(conn, rtvResults, Request.QueryString.Get("SN"));
                glogDate = pt.glogDate;
            protected void btnExportTree_Click(object sender, EventArgs e)
                string sFileName = System.IO.Path.GetRandomFileName();
                string sGenName = "DXiConfigInfo.csv";
                if (Request.QueryString.Get("SN") != null)
                    DateTime dt;
                    DateTime.TryParse(glogDate, out dt);
                    sGenName = "DXiConfigInfo_" + Request.QueryString.Get("SN") + "_LogDate_" + dt.ToString("yyyyMMddHHmm") + ".csv";
                StreamWriter sw = new StreamWriter(Server.MapPath("TextFiles/" + sFileName + ".csv"), false, System.Text.Encoding.UTF8);
                exportToXml(sw, rtvResults);
                System.IO.FileStream fs = null;
                fs = System.IO.File.Open(Server.MapPath("TextFiles/" +
                         sFileName + ".csv"), System.IO.FileMode.Open);
                byte[] btFile = new byte[fs.Length];
                fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
                Response.AddHeader("Content-disposition", "attachment; filename=" +
                Response.ContentType = "application/octet-stream";
            public static void exportToXml(StreamWriter sr, RadTreeView parent)
                foreach (RadTreeNode node in parent.Nodes)
                    if (node.Nodes.Count > 0 && node.Expanded)
                        parseNode(sr, node);
            private static void parseNode(StreamWriter sr, RadTreeNode parent)
                foreach (RadTreeNode child in parent.Nodes)
                    for (int i = 0; i < child.Level; i++)
                    if (child.Nodes.Count > 0 && child.Expanded)
                        parseNode(sr, child);
  2. Bozhidar
    Bozhidar avatar
    1096 posts

    Posted 28 Feb 2013 Link to this post

    Hello Casey,

    This happens because you are rebinding the TreeView on each postback in the Page_Load event.

    There are two possible solutions.
    1. If you don't really need to rebind the treeview on each postback (the nodes will be persisted if you add them in the Page_Load event), you can bind it only on initial load. Like so:
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            //bind tree

    2. The second solution is to bind the TreeView in the Page_Init event instead.

    Kind regards,
    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.
  3. Telerik UI for ASP.NET Core is out
  4. Casey
    Casey avatar
    14 posts
    Member since:
    May 2011

    Posted 28 Feb 2013 Link to this post

    Thank you!  Thank you!  Thank you!  I knew it had to be something silly but I wasn't able to find it.
Back to Top