views:

3084

answers:

2

I have a ASP.Net TreeView control that I am binding to an XML data source. I'm wanting to be able to control which nodes are expanded and which ones are collapsed in the XML definition file. The Expanded='' doesn't work for me though. In the following simple example, I want Node 2 to be fully expanded.

ASP Page...

<asp:XmlDataSource ID="oXmlDataSource" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" EnableViewState="false" DataSourceID="oXmlDataSource"></TreeView>

Code Behind...

oXmlDataSource.Data = MyXMLString;
oXmlDataSource.XPath = "/Tree/Node";

Here is the XML...

<?xml version='1.0' encoding='utf-8' ?>
<Tree Text='example.aspx' Href='example.aspx'>
      <Node Text='Example Node 1' Href='0800200c9a66.aspx' Expanded='false'></Node>
      <Node Text='Example Node 2' Href='0800200c9a66.aspx' Expanded='true'>
         <Node Text='Example Node 3' Href='0800200c9a66.aspx' Expanded='false'></Node>
         <Node Text='Example Node 4' Href='0800200c9a66.aspx' Expanded='false'></Node>
         <Node Text='Example Node 5' Href='0800200c9a66.aspx' Expanded='false'></Node>
         <Node Text='Example Node 6' Href='0800200c9a66.aspx' Expanded='false'></Node>
      </Node>
</Tree>
A: 

Hmm...well, using "Expanded" won't get you anywhere by default, since the DataBinding won't even look at that...I suppose you could set up some sort of custom DataBinding, but my suggestion would be to hook into the OnTreeNodeDataBound event and expand/collapse as needed there, although your mileage may vary with that approach.

Your problem revolves around the way the data binding for the treeview happens:If you've got Reflector handy, check out the DataBindRecursive method on the TreeView control. If you don't have Reflector handy, go install it. :)

JerKimball
A: 

Unfortunately you can't do this in the XML. It is a short coming of the control. You will need to populate the TreeView with the XML and then traverse all of the nodes recursively and expand the branch that you need. Try the following:

Add the OnPreRender attrobite to the TreeView control...

<asp:XmlDataSource ID="oXmlDataSource" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" EnableViewState="false" DataSourceID="oXmlDataSource" OnPreRender="TreeView1_PreRender"></TreeView>

Then add the following to the code behind (I recommend adding some testing and adding try/catches)...

protected void TreeView1_PreRender(object sender, EventArgs e)
{
    SelectCurrentPageTreeNode(TreeView1);
}

private void SelectCurrentPageTreeNode(TreeView tvTreeView)
{
    tvTreeView.CollapseAll();

    if (Request.Url.PathAndQuery != null)
    {
        ExpandTreeViewNodes(tvTreeView, Request.Url.PathAndQuery);
    }
}

private TreeNode ExpandTreeViewNodes(TreeView tvTreeView, string sPathAndQuery)
{
    if (tvTreeView != null)
    {
        if (!string.IsNullOrEmpty(sPathAndQuery))
        {
            sPathAndQuery = sPathAndQuery.ToLower();
            {
                TreeNode tnWorkTreeNode = null;

                for (int iLoop = 0; iLoop < tvTreeView.Nodes.Count; iLoop++)
                {
                    tvTreeView.Nodes[iLoop].Expand();

                    tvTreeView.Nodes[iLoop].Selected = true;
                    if (tvTreeView.Nodes[iLoop].NavigateUrl.ToLower() == sPathAndQuery)
                    {
                        return (tvTreeView.Nodes[iLoop]);
                    }
                    else
                    {
                        tnWorkTreeNode = ExpandTreeViewNodesR(tvTreeView.Nodes[iLoop], sPathAndQuery);
                    }

                    if (tnWorkTreeNode != null)
                    {
                        return (tnWorkTreeNode);
                    }

                    tvTreeView.Nodes[iLoop].Collapse();
                }
            }
        }
    }

    return (null);
}

private static TreeNode ExpandTreeViewNodesR(TreeNode tvTreeNode, string sPathAndQuery)
{
    TreeNode tnReturnTreeNode = null;

    if (tvTreeNode != null)
    {
        tvTreeNode.Expand();
        if (tvTreeNode.NavigateUrl.ToLower() == sPathAndQuery)
        {
            return (tvTreeNode);
        }
        else
        {
            tnReturnTreeNode = null;

            for (int iLoop = 0; iLoop < tvTreeNode.ChildNodes.Count; iLoop++)
            {
                tvTreeNode.ChildNodes[iLoop].Selected = true;
                tnReturnTreeNode = ExpandTreeViewNodesR(tvTreeNode.ChildNodes[iLoop], sPathAndQuery);

                if (tnReturnTreeNode != null)
                {
                    return (tnReturnTreeNode);
                }
            }
            tvTreeNode.Collapse();
        }
    }
    return (null);
}