Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 101947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:52:09+00:00 2026-05-11T00:52:09+00:00

I have a ASP.Net TreeView control that I am binding to an XML data

  • 0

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> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:52:10+00:00Added an answer on May 11, 2026 at 12:52 am

    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); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
Let say I have the following desire, to simplify the IConvertible's to allow me
I want to have generalised email templates. Currently I have multiple email templates with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.