【问题标题】:Why doesn't my TreeView update?为什么我的 TreeView 没有更新?
【发布时间】:2010-04-05 19:12:51
【问题描述】:

我在带有自定义 XmlDataSource 的页面上使用 ASP.NET TreeView。当用户点击树的一个节点时,一个DetailsView 会弹出并编辑一堆关于底层对象的东西。所有这些都正常工作,并且底层对象在我的后台对象管理类中得到更新。但是,我的 TreeView 只是没有更新显示。要么立即(我想要它),要么在整页重新加载时(这是我需要它达到的最小有用级别)。我对XmlDataSource 的子类化很差吗?我真的不知道。谁能指点我一个好的方向?

标记看起来像这样(去除了箔条):

<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>

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\" />";
    }
}

}

【问题讨论】:

  • 你的 page_load 是什么样的?
  • 目前什么都没有。最终我会在那里进行一些样式调整(tree.Font.Name、tree.ShowLines 等),但现在代码隐藏只包含空函数。
  • 如果我在数据源中禁用缓存,它至少会在整个页面重新加载时显示节点名称的更改。但是,当详细信息视图进行更新时,它不会显示更改。

标签: c# asp.net treeview xmldatasource


【解决方案1】:

好吧,看来数据绑定并没有像我想象的那样工作。

我的解决方案是绑定到我的 detailsview 数据源的 OnUpdate 和 OnInsert 事件 - 当以更改树的方式更新项目时,我在树视图的数据源上显式调用 DataBind。似乎必须有更清洁的方法,但我找不到。

【讨论】:

    猜你喜欢
    • 2018-03-21
    • 2011-10-10
    • 2018-03-13
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-24
    • 2017-11-13
    相关资源
    最近更新 更多