Why doesn't my TreeView update?
I'm using an ASP.NET TreeView
on a page with a custom XmlDataSource
. When the user clicks on a node of the tree, a DetailsView
pops up and edits a bunch of things about the underlying object. All this works properly, and the underlying object gets updated in my background object-management classes. However, my TreeView
just isn't updating the display. Either immediately (which I would like it to), or on full page re-load (which is the minimal useful level I need it to be at). Am I subclassing XmlDataSource
poorly? I really don't know. Can anyone point me in a good direction?
The markup looks about like this (chaff removed):
<data:DefinitionDataSource runat="server" ID="DefinitionTreeSource" RootDefinitionID="uri:1"></data:DefinitionDataSource>
<asp:TreeView ID="TreeView" runat="server" DataSourceID="DefinitionTreeSource">
<DataBindings>
<asp:TreeNodeBinding DataMember="definition" TextField="name" ValueField="id" />
</DataBindings>
</asp:TreeView>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="Id" DataSourceID="DefinitionSource" DefaultMode="Edit">
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Wrap="false" SortExpression="Name" />
<asp:CommandField ShowCancelButton=开发者_如何学编程"False" ShowInsertButton="True" ShowEditButton="True"
ButtonType="Button" />
</Fields>
</asp:DetailsView>
And the DefinitionTreeSource
code looks like this:
public class DefinitionDataSource : XmlDataSource
{
public string RootDefinitionID
{
get
{
if (ViewState["RootDefinitionID"] != null)
return ViewState["RootDefinitionID"] as String;
return null;
}
set
{
if (!Object.Equals(ViewState["RootDefinitionID"], value))
{
ViewState["RootDefinitionID"] = value;
DataBind();
}
}
}
public DefinitionDataSource() { }
public override void DataBind()
{
base.DataBind();
setData();
}
private void setData()
{
String defXML = "<?xml version=\"1.0\" ?>";
Test.Management.TestManager.Definition root =
Test.Management.TestManager.Definition.GetDefinitionById(RootDefinitionID);
if (root != null)
this.Data = defXML + root.ToXMLString();
else
this.Data = defXML + "<definition id=\"null\" name=\"Set Root Node\" />";
}
}
}
Alright well it seems that databinding just doesn't work quite how i thought it did.
My solution was to tie in to the OnUpdate and OnInsert events for my detailsview data source - when an item is updated in a way that will change the tree i call DataBind explicitly on the treeview's data source. It seems like there must be a cleaner way but i can't find it.
精彩评论