Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Programmatically select a Treeview node?
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
ASP.NET
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01373668
Message ID:
01374227
Vues:
20
>>So far the TreeView control in ASP.NET 2.0 seems even more half-baked than most of the ASP.NET controls.
>>
>>I'm working on an application that uses a treeview for navigation through several layers of heirarchical data. The application inserts/updates/deletes records in that data.
>>
>>Building/rebuilding and displaying the treeview is no problem.
>>
>>Problem comes when I am deep in the treeview and edit a record and want to rebuild the treeview to show updated data. I want to rebuild the treeview and then programmatically select the node I was working on (or select the parent if I deleted or inserted a node).
>>
>>How do I programmatically navigate back to and select a given node after reloading a treeview?
>>
>
>For some reason I don't seem to think they have the equivalent of EnsureVisible like the COM treeview does. But, it still should be relatively simple to do this. You need to get the selected node (exactly how depends on what event(s) have fired, etc.). But basically it's:
>
>
>TreeNode node = this.TreeView1.SelectedNode;
>if (node != null)
>{
>   // Code here.
>}
>
>
>You may need to search for the selected node (depending on what's happened before it in your code) - you can use FindNode for that (note: this code actually doesn't make sense on it's own - I'm just using it to show how you can find a specific node if you don't have a reference to it already):
>
>
>string valuePath = this.TreeView1.SelectedNode.ValuePath;
>TreeNode node = this.TreeView1.FindNode(valuePath);
>if (node != null)
>{
>     node.Selected = true;
>     this.ExpandParent(node);
>}
>
>
>ExpandParent() can just be some recursive code which does this:
>
>
>protected void ExpandParent(TreeNode node)
>{
>    if (node.Parent != null)
>    {
>        node.Parent.Expand();
>        this.ExpandParent(node.Parent);
>    }
>}
>
This is almost exactly what I wound up doing (see my message a few minutes before yours in the thread).

It wasn't all that hard once I figured it out, but it wasn't documented anywhere that I could find (google searches).
____________________________________

Don't Tread on Me

Overthrow the federal government NOW!
____________________________________
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform