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
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">
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
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);
}
}
}
}