I’m trying to create a TreeView based on an ID passed through a query string. Here’s my TreeView control:
<asp:TreeView ID="tvDirectories" runat="server" DataSourceID="xdsDirectories">
<DataBindings>
<asp:TreeNodeBinding DataMember="DirectoryNode"
ValueField="Id" TextField="Title" />
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource ID="xdsDirectories" runat="server" />
When the page is loaded, I take the ID, do some behind-the-scenes things to get the xml I want, and then I bind it to the XmlDataSource. Here is that code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string id = Request.QueryString["id"];
StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
GetDirectories(id).WriteXml(xw); // my function
xw.Flush();
xw.Close();
string xml = sb.ToString();
xdsDirectories.Data = xml;
xdsDirectories.DataBind();
}
}
Here is what the returned xml from my function looks like:
<?xml version="1.0" encoding="utf-16"?>
<DirectoryNode Id="1" Title="Directory1">
<DirectoryNode Id="24" Title="SubDirectory1" />
<DirectoryNode Id="57" Title="SubDirectory2" />
</DirectoryNode>
So the first time I load mypage.aspx?id=1, it works fine – the tree loads correctly with all the Ids and Titles. But if I load mypage.aspx?id=2, it loads the same data as if I passed in id=1. And it does the same for any request after that with any other ids.
I’ve gone through the debugger and checked the data that is being binded to the XmlDataSource – it all looks fine, but the TreeView displays the data from the first request.
If I take out the DataSourceID attribute, reload the page so that the TreeView shows nothing, add the DataSourceID attribute back, and then load mypage.aspx?id=2, then the tree will show the correct data. But then any request with any other id after that will only show data as if I passed in id=2. So something weird is going on.
Is the data being cached somewhere, or am I doing something wrong?
Have you tried to set caching disabled?
EnableCaching=”false” on the XmlDataSource