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

  • Home
  • SEARCH
  • 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 8587661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:37:20+00:00 2026-06-11T22:37:20+00:00

I have a tree view that I’m dynamically loading on the PageLoad event. Here

  • 0

I have a tree view that I’m dynamically loading on the PageLoad event. Here is my tree view and data source:

<asp:TreeView ID="tv" runat="server" DataSourceID="xds"
    SelectedNodeStyle-ForeColor="Red" NodeStyle-ForeColor="Black">   
    <DataBindings>
        <asp:TreeNodeBinding DataMember="Node"
            ValueField="Id" TextField="Title"  />
    </DataBindings>
</asp:TreeView>
<asp:XmlDataSource ID="xds" runat="server" EnableCaching="false" />

This is the code I run on PageLoad

StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);

WriteTreeViewXml(xw);
xw.Flush();
xw.Close();

xds.Data = sb.ToString();
xds.DataBind();

tv.Nodes[0].Selected = true;  // error here

This works fine, the tree view gets loaded correctly. Now I’m trying to add styling to the SelectedNode. This works when I click on a specific node, but the root node isn’t styled when the page loads. So I’m trying to set the root node to selected on PageLoad

I get this error when I try to set the root node as selected:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

For some reason the tree view is empty after I bind the data source.

Does anyone know how I can do this?

  • 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. Editorial Team
    Editorial Team
    2026-06-11T22:37:21+00:00Added an answer on June 11, 2026 at 10:37 pm

    You need to call tv.Databind() after the line that calls xds.DataBind(); Otherwise the control isn’t being bound to the datasource. I believe it would do this for you if you were declaratively setting the XmlDataSource (to a static file, for example), but since you are manually populating it, you’ll need to manually call the control’s DataBind method as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hypothetical tree view that contains this data: RootNode Leaf vein SecondRoot
I have a MainWindow that contains a window with a TreeView. The tree view
I have a table that contains my node data in a tree view. Each
I have this Tree View that looks like this: <TreeView Grid.Column=0 Grid.Row=2 MinHeight=100 MinWidth=100
I have a treeview control that functions like a folder browser. Because loading the
I have three tree view controls which house different (but mostly similar data), as
I have a tree view that is acting as a selection provider. In response
I have a tree view that contains 3 roots and each root contains 3
I currently have a simple tree view that contains one parent node with multiple
I have an app written in XUL which contains a custom tree view that

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.