简体   繁体   English

为什么我的TreeView不更新?

[英]Why doesn't my TreeView update?

I'm using an ASP.NET TreeView on a page with a custom XmlDataSource . 我在具有自定义XmlDataSource的页面上使用ASP.NET TreeView When the user clicks on a node of the tree, a DetailsView pops up and edits a bunch of things about the underlying object. 当用户单击树的节点时,将弹出DetailsView并编辑有关基础对象的许多内容。 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. 但是,我的TreeView只是不更新​​显示。 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? 我不能很好地XmlDataSource子类吗? 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: DefinitionTreeSource代码如下所示:

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. 我的解决方案是为我的detailsview数据源绑定到OnUpdate和OnInsert事件-当以一种会更改树的方式更新项目时,我会在树视图的数据源上显式调用DataBind。 It seems like there must be a cleaner way but i can't find it. 似乎必须有一种更清洁的方法,但我找不到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM