Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

TreeView

Back to Forum: Tech Off
  • User profile image
    ScanIAm

    I've looked on the asp.net forums, and only found one similar question, and the answer wasn't really valid, so here goes:

    I've got a webusercontrol that simply contains 1 TreeView control and some backing code.  On pageload of the usercontrol, I fill the TreeView with some data and I've set the

    TreeView.ExpandDepth = 1.

    This data will likely be stale, so I've also added a button that will refresh the data by:

    TreeView.Nodes.Clear();
    TreeView.Nodes.Add(FunctionThatReturnsFreshData());

    The weird part is that once I've done this, the TreeView's expand depth becomes 0.  I've tried manually changing it, but I suspect that when I called TreeView.Nodes.Clear(), the internal value was changed, and I can't figure out how to get it back to 1 again.

    Any ideas?

  • User profile image
    odujosh

    Reflector is your friend. The reason why it does not keep the ExpandDepth is when you

    clear the the TreeNodeCollection of the TreeView it wipes out the ViewState of the TreeView.
    Thus you will have to reset anything that custom set. As you can see by ExpandDepth code below it is stored between postbacks
    in the ViewState. The Default Value set in the attribute above the declaration is -1 meaning do not expand to any level.



    Reflector is available here : http://www.aisto.com/roeder/dotnet/Download.aspx?File=Reflector

    The Reflector Code that shows you how it all works.

    Josh











    public void Clear() { if (this.Count != 0) { if (this._owner != null) { TreeView owner = this._owner.Owner; if (owner != null) { if (owner.CheckedNodes.Count != 0) { owner.CheckedNodes.Clear(); } for (TreeNode c = owner.SelectedNode; c != null; c = c.Parent) { if (this.Contains(c)) { owner.SetSelectedNode(null); break; } } } } foreach (TreeNode node2 in this._list) { node2.SetParent(null); } this._list.Clear(); this._version++; if (this._isTrackingViewState) { this.Log.Clear(); } this.Log.Add(new LogItem(LogItemType.Clear, 0, this._isTrackingViewState)); } }










    [TypeConverter(typeof(TreeViewExpandDepthConverter)), DefaultValue(-1), WebSysDescription("TreeView_ExpandDepth"), WebCategory("Behavior")] public int ExpandDepth { get { object obj2 = this.ViewState["ExpandDepth"]; if (obj2 == null) { return -1; } return (int) obj2; } set { this.ViewState["ExpandDepth"] = value; } }

  • User profile image
    ScanIAm

    Right, I guess I wasn't as clear as I should have been.  You are correct as to why the TreeView.ExpandDepth get's cleared, but I can't figure out how to re-set it again.  I've tried setting it after I've loaded the data in, and it doesn't reset.

    I had a similar problem programmatically setting the selection dynamically in a combobox, and it turned out that I need to iterate through each listitem and manually 'select' the right one.  I'm guessing I might have to do the same on the TreeView with the nodes....

  • User profile image
    JohnAskew

    I'm not sure what locale (Taiwan?) this snippit is from but it may confirm your existing plan:

    <%@ Page Language="C#" %>

    <script runat="server">

      void Data_Bound(Object sender, TreeNodeEventArgs e)
      {

        // Determine the depth of a node as it is bound to data.
        // If the depth is 1, expand the node.
        if(e.Node.Depth == 1)
        {

          e.Node.Expanded = true;

        }
        else
        {

          e.Node.Expanded = false;

        }
      }

    </script>

    <html>
      <body>
        <form runat="server">
       
          <h3>TreeNode Expanded Example</h3>
       
          <asp:TreeView id="BookTreeView"
            DataSourceID="BookXmlDataSource"
            OnTreeNodeDataBound="Data_Bound"
            runat="server">
            
            <DataBindings>
              <asp:TreeNodeBinding DataMember="Book" TextField="Title"/>
              <asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/>
              <asp:TreeNodeBinding DataMember="Section" TextField="Heading"/>
            </DataBindings>
            
          </asp:TreeView>
         
          <asp:XmlDataSource id="BookXmlDataSource" 
            DataFile="Book.xml"
            runat="server">
          </asp:XmlDataSource>
       
        </form>
      </body>
    </html>

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.