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

    Hello,

    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?

    Thanks,
    Casey
    <%@ 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <div>
            <telerik:RadButton ID="btnExportTree" runat="server" Text="Export Tree" OnClick="btnExportTree_Click"
                Visible="true">
            </telerik:RadButton>
            <telerik:RadTreeView ID="rtvResults" runat="server" Visible="true">
            </telerik:RadTreeView>
        </div>
        </form>
    </body>
    </html>
    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);
                rtvResults.Nodes.Clear();
                conn.Open();
                PopulateTree pt = new PopulateTree();
                pt.AddNodes(conn, rtvResults, Request.QueryString.Get("SN"));
                glogDate = pt.glogDate;
                conn.Close();
            }
     
            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);
                sw.Close();
     
                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));
                fs.Close();
                Response.AddHeader("Content-disposition", "attachment; filename=" +
                                   sGenName);
                Response.ContentType = "application/octet-stream";
                Response.BinaryWrite(btFile);
                Response.End();
            }
            public static void exportToXml(StreamWriter sr, RadTreeView parent)
            {
                foreach (RadTreeNode node in parent.Nodes)
                {
                    sr.WriteLine(node.Text);
                    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++)
                        sr.Write(",");
                    sr.WriteLine(child.Text);
                    if (child.Nodes.Count > 0 && child.Expanded)
                        parseNode(sr, child);
                }
            }
        }
    }
  2. Bozhidar
    Admin
    Bozhidar avatar
    1102 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,
    Bozhidar
    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. UI for ASP.NET Ajax is Ready for VS 2017
  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