.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

TreeView Set Selected Node

Posted By:      Posted Date: December 04, 2010    Points: 0   Category :Windows Application
You cannot vote on your own post

I am using Visual Studio 2008 Standard Edition, Visual C++, Windows Forms Application.

I would like to set a treeview node to be "Selected" without clicking on it.

I know that you can click on a Node and that Node then becomes the "Selected" Node but how do you force a Node to be "Selected" (with code) without clicking on it?


View Complete Post

More Related Resource Links

How to handle treeview selected node changed event



From my below sample code, I don't want to use Sessions to handle each click event inside treeview selected node changed.

Secondly, if you suggest me to use treenode.SelectedValue then I would say my values are dyanmically generated.

Everytime a node a clicked, the selected node value is sent in the next request xml to get response and xml schema is different for each click. How do I handle this each click event other than using session variables. I thought of using Request.Item (and integrating with URL redirect property) but I don't want to use that.


Dim trNode As New TreeNode
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then        
            trNode.Text = "Not Postback"
            Session("Flag") = 1
        End If
    End Sub

Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TreeView1.SelectedNodeChanged
        If Session.Item("Flag") = 1 Then
            trNode.Text = "Is PostBack"
            Session("Flag") = 2
        End If

treeview selected node font style does not change


Hi All,

I have a treeview and I am giving the node style in code behind.

Treeview design is as below:

<asp:TreeView ID="tvIARHierarchies" runat="server" NodeIndent="15" ShowExpandCollapse="true"
                                        ShowLines="True" Width="98%" OnSelectedNodeChanged="tvIARHierarchies_SelectedNodeChanged">
                                        <ParentNodeStyle Font-Bold="False" />
                                        <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />

Determine node type selected in TreeView and reading property values


I am hoping that someone can help me get past some problems I am having when using the WPF TreeView control.  I have a TreeView that at most can have 3 levels.  The levels are defined as Project, Discipline, Person which would look like the following:

+ Project

    + Discipline

        - Person

My TreeView is bound to a database using two HierarchicalDataTemplate's and then one DataTemplate (the binding is done in the XAML).

What I would like to get is the following information when selecting an item:

  1. Read the selected value (text of the selected node)
  2. WPF Selected xml node from treeview, I want to populate a datagrid with that node and all child node

    I have populated a tree view from a xml.  I know want to take the node and all children that has been selected by double clicking on the tree node and populate a data grid.

Treeview: Select all children if Parent is selected etc...



I'm using a Treeview with  ShowCheckBoxes="All" , so basically I have a tree with all CheckBox Nodes. I'm using the TreeView inside an update panel, and populate the Tree code behind dynamically.

My requrements are the following:

1) Select all the children if parent is selected,

2) When parent is selected, and if we de-select a child, set parent unselected

3) Track if new nodes were selected and previously selected nodes were de-selected (becasue I need to update the database according to the selections in the Tree)

For above 1, I do the following:

protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        foreach (TreeNode node in TreeView1.Nodes)
            TreeNodeCollection col = node.ChildNodes;
            //select all children when the parent is checked
            if (col != null && node.Checked)

(Imperfect) Solution to TreeView Postback / Client-Side Node Selection Issue


I've seen this question posted on all corners of the web... "How do I disable "AutoPostBack" on the ASP.NET 2.0 TreeView control?!"  Unfortunately, most of the answers either don't work, miss the point (don't work right), or boil down to "You can't do it."  I happen to know that this behavior worked perfectly in the IEWebControls TreeView that Microsoft released as an add-on prior to ASP.NET 2.0, however.  So I set out to find a solution for the ASP.NET 2.0 TreeView issue.

In a nutshell, the behavior that I and others are looking for is to have a TreeView rendered in a page that allows the user to expand, contract, and select nodes within the TreeView without causing a PostBack to the server.  This implies two things: (1) the most recently-selected node should be recorded as the currently-selected node [i.e., at PostBack time, the most-recently selected node is actually represented as the SelectedNode]; and (2) the selected node should exhibit the SelectedNodeStyle as specified in the TreeView declaration.

It appears that the TreeView engineers saw only two possible outcomes from selecting a leaf node in a tree: (1) immediately posting back to the server, or (2) immediately navigating to another web page.  They apparently did not consider option (3) just allowing selection in-place on the client, while waiting for a click on so

How can i update treeview root node every time based on the record entries

Hi all, I will have an mdi form and with a treeview which has it's initial property as visible =false. If i select an option from menu a child form will exists i will fill some details and click on save when ever click on save i will make the root node of the treeview to some name as File[batch count:0]. Now if i select another option from menu another form will appear and if i fill it with some details and click on save i would like to change the root node name to File[batch count:1] and a child node should exists with some name

VS2008 and C#: How to create a short cut for a selected item in a ListView/TreeView ?

How to create a short cut for a selected item from a ListView/TreeView ? Is it possible to provide a smaple code ? Thanks,

On escape key while editing a treeview node

On hitting escape key while editing a treeview i would like to cancel the editing a would like to show the default name that was previously there. Where should i handle this can any one tell me please..    

Last node in TreeView

Hi everybody.... How are you.... i think everything is all right...... now i have a question.... how to get the last node in treeview ??????? Thankssss.....

Treeview - how to change node icon depending on node contents

I have a treeview in an MVVM application: <TreeView ItemsSource="{Binding RootNodes}"> <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> <Setter Property="FontWeight" Value="Normal" /> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="FontWeight" Value="Bold" /> </Trigger> </Style.Triggers> </Style> </TreeView.ItemContainerStyle> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Children}"> <TextBlock Text="{Binding NodeName}" /> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> The treeview is bound to an ObservableCollection called RootNodes. Elements in the RootNodes collection expose a member "NodeName" which is the text for the nodes in the treeview at runtime. They also expose a member called "NodeType", effectively an enumeration that indicates what type of icon should be displa

Last node in TreeView

Hi everybody.... How are you.... i think everything is all right...... now i have a question.... how to get the last node in treeview ??????? Thankssss.....

drag and drop treeview node to textarea in web forms

Hi We have an asp.net treeview control and a texarea. The childnodes of treeview need to be draggable and can be dropped into txtarea.   Please let us know how it will be achieved through JQuery?

Treeview node to add onclick event to download files

 Hi, I would like to know how to add onclick evend to a TreeNode so that the file can be downloaded.   How can I implement below sample code to a TreeNode? <a href="#" onclick="popup('/directory1/Somefile.doc'); return false;" target="download"> Somefile </a> <script language="javascript"> function popup(url) { popWin = window.parent.open(url, 'popWindow', 'height=900, width=900, toolbar=no, menubar=yes, scrollbars=yes, titlebar=no, resizable=yes,location=0, directories=no, status=no'); } </script> I have TreeNodePopulate event to populate nodes like this: protected void DocRepository_TreeNodePopulate(object sender, TreeNodeEventArgs e) { .... String[] files = Directory.GetFiles(fullPath); foreach(String file in files) { TreeNode newNode  = new TreeNode(Path.GetFileName(file), Path.GetFileName(file)); node.ChildNodes.Add(newNode); } }   Thank you in advance.              

Display error if a treeview root node contains a child node

I am having a treeview initially with a root node when form is loaded. I will add child node as some.txt file at the runtime by selecting an option as Addnew from contextmenu which was displayed when the user right clicks on the root node. Now what i need is if the tree has child node appended to the Root and if user tries to create a new node by clicking the option addnew from context menu i would like to display an error as only one child allowed.   Can any one help

TreeView node visually unselected after selecting tree node and then clicking soem other field



I am using .NET 3.5 (VS 2008).  I have a SplitContainer with a TreeView on one side and a form with various fields, tabs, etc on the other.  When I select one of the tree nodes, the node is highlighted.  The node loses its highlighting when the user clicks on one of the tabs or enters a field on the other side of the SplitContainer.  Although no highlighting is visible, the treeView.SelectNode() still returns the proper node, so it appears that the tree node is still selected, just not visually highlighted.  The problem is that without the highlighting to distinguish which node is selected, the user can easily get lost.  Is there a build-in mechanism to maintain some sort of highlighting on the selected node when the cursor is moved to another form.  For example, in Windows Explorer, if you select a tree node, the node is highlighted in a dark blue.  If you were to then place the cursor in the Address box, the selected node changes to have a beige color.  This still shows which node was selected.

Is there a built-in solution (ie some property to set on the tree view or nodes) that would help fix my issue or do I have to build my own solution to mimic something like what Windows Explorer does?  I wanted to avoid having to put event handlers on all tabs and form fields that would be used to &

TreeView, ContextMenu, how do I get the SelectedItem's data Node/object?


Hi all,

I've implemented a tree and have bound it to my treeview. the binding is fine, but id like to add "add child" "remove node" functionality in a context menu for a selected item. I can not for the life of me figure out how to do this. My code is below. please help me. please point me in the right direction.


<TreeView Name="treeView" Margin="10,10,14,10" BorderThickness="2" ItemsSource="{Binding Nodes}">
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend