TreeView caching


G

Guest

Using Visual Studio 2005 SP1 I am attempting to dynamically load a treeview
control.
I create an XmlDataSource and then load the data source using
XmlDataSource.Data. I Load my XML string into this property using the code
shown below. However if I change the values in the XML string that I am
loading into XmlDataSource.Data and then run the web, the changes do not show
up. The only way to get the changes to show up is to rebuild the solution.

Am I doing something wrong or is something being cached somewhere that I am
not aware of? Is there a way to cause the changes to be reflected correctly
just by pressing F5 and letting the web run?

Thanks,

Leslie

XmlDataSource xds = new XmlDataSource();
xds.Data = @"<?xml version=""1.0"" encoding=""iso-8859-1""?>
<books1 title=""My books"" Nav=""simple.aspx"">
<computerbooks1 Nav=""simple.aspx"">
<book title=""Secrets of Silicon Valley"" author=""Sheryl
Hunter"" Nav=""simple.aspx"" />
<book title=""Straight Talk About Computers"" author=""Dean
Straight"" Nav=""simple.aspx""/>
<book title=""You Can Combat Computer Stress!"" author=""Marjorie
Green"" Nav=""simple.aspx""/>
</computerbooks1>
<cookbooks Nav=""simple.aspx"">
<book title=""Silicon Valley Gastronomic Treats"" author=""Innes
del Castill"" Nav=""simple.aspx""/>
</cookbooks>
</books1>"
;

TreeNodeBinding tnbBooks = new TreeNodeBinding();
tnbBooks.DataMember = "books1";
tnbBooks.Depth = 0;
tnbBooks.TextField = "title";
//tnbBooks.NavigateUrlField = "Nav";
TreeNodeBinding tnbBook = new TreeNodeBinding();
tnbBook.DataMember = "book";
tnbBook.TextField = "title";
tnbBook.Target = "_self";
tnbBook.NavigateUrlField = "Nav";
TreeView5.DataBindings.Add(tnbBook);
TreeView5.DataBindings.Add(tnbBooks);
TreeView5.DataSource = xds;
TreeView5.DataBind();
 
Ad

Advertisements

S

Steven Cheng[MSFT]

Hello Leslie,

From your description, you're dynamically create a XmlDataSource and bind
it to a TreeView control. However, you found that when you change the
XmlDataSource content, the TreeView's node structure doesn't reflect the
data changes, correct?

Based on my research, the problem is due to the XmlDataSource by default
enable caching of the underlying XML Data. If you statically add it onto
webform , you can set its "EnableCaching" to false in property window.
Since you're dynamically create the XmlDataSource, you can set this
property to "false" in code after create it. Here is a test page I used in
my local test environment:


========aspx=================
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>template1</asp:ListItem>
<asp:ListItem>template2</asp:ListItem>
</asp:DropDownList>
<asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView>
<asp:XmlDataSource ID="XmlDataSource1"
runat="server"></asp:XmlDataSource>

</div>
</form>
</body>
</html>

========code behind===========


public partial class nav_DynamicTreeView : System.Web.UI.Page
{

public const string tmpData1 = @"<?xml version=""1.0""
encoding=""iso-8859-1""?>
<books1 title=""My books1"" Nav=""simple.aspx"">
<computerbooks1 Nav=""simple.aspx"">
<book title=""Secrets of Silicon Valley1"" author=""Sheryl
Hunter"" Nav=""simple.aspx"" />
<book title=""Straight Talk About Computers1"" author=""Dean
Straight"" Nav=""simple.aspx""/>
<book title=""You Can Combat Computer Stress!1""
author=""Marjorie
Green"" Nav=""simple.aspx""/>
</computerbooks1>
<cookbooks Nav=""simple.aspx"">
<book title=""111Silicon Valley Gastronomic Treats1""
author=""Innes
del Castill"" Nav=""simple.aspx""/>
</cookbooks>
</books1>";

public const string tmpData2 = @"<?xml version=""1.0""
encoding=""iso-8859-1""?>
<books1 title=""My books2"" Nav=""simple.aspx"">
<computerbooks1 Nav=""simple.aspx"">
<book title=""Secrets of Silicon Valley2"" author=""Sheryl
Hunter"" Nav=""simple.aspx"" />
<book title=""Straight Talk About Computers2"" author=""Dean
Straight"" Nav=""simple.aspx""/>
<book title=""You Can Combat Computer Stress!2""
author=""Marjorie
Green"" Nav=""simple.aspx""/>
</computerbooks1>
<cookbooks Nav=""simple.aspx"">
<book title=""222Silicon Valley Gastronomic Treats2""
author=""Innes
del Castill"" Nav=""simple.aspx""/>
</cookbooks>
</books1>";



protected void Page_Load(object sender, EventArgs e)
{
SetupTree();
}

protected void SetupTree()
{





XmlDataSource xds = new XmlDataSource();
xds.EnableCaching = false;



if (DropDownList1.SelectedIndex == 0)
{
xds.Data = tmpData1;
}
else
{
xds.Data = tmpData2;
}

TreeNodeBinding tnbBooks = new TreeNodeBinding();
tnbBooks.DataMember = "books1";
tnbBooks.Depth = 0;
tnbBooks.TextField = "title";


TreeNodeBinding tnbBook = new TreeNodeBinding();
tnbBook.DataMember = "book";
tnbBook.TextField = "title";
tnbBook.Target = "_self";
tnbBook.NavigateUrlField = "Nav";
TreeView1.DataSource = null;
TreeView1.DataBind();

TreeView1.DataBindings.Clear();
TreeView1.DataBindings.Add(tnbBook);
TreeView1.DataBindings.Add(tnbBooks);
TreeView1.DataSource = xds;
TreeView1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
SetupTree();
}
}
==================================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ad

Advertisements

S

Steven Cheng[MSFT]

That's cool! :)

Have a nice day!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Treeview from database 0

Top