C# – treeview node click events c# asp .net

asp.netcclickeventstreeview

I have a web application using TreeView control in C# asp .net.

I have set up the event handlers for 2 events
1) onselectednodechanged
2) ontreenodeexpanded

      <asp:Panel ID="PanelTreeView" runat="server" height="510" Width="270" ScrollBars="Auto">
        <asp:TreeView ID="TreeViewTabs" runat="server" Font-Size="Medium" NodeIndent="10" 
           onselectednodechanged="TreeViewTabs_SelectedNodeChanged" 
             ontreenodeexpanded="TreeViewTabs_TreeNodeExpanded">
           <LeafNodeStyle Font-Overline="False" Font-Size="Small" NodeSpacing="0px" 
                VerticalPadding="0px" />
            <NodeStyle NodeSpacing="0px" VerticalPadding="0px" />
           <SelectedNodeStyle ForeColor="Red" />
        </asp:TreeView>                      
     </asp:Panel>

The problem is that if the user clicks on a selected node in the tree, the web app posts back and I don't know which event I should catch. I know it is definitely not "onselectednodechanged" event. I also tried event handler "onTreeNodeCheckChanged", it does not fire that event either. I know that the post back will go through Page_Load() handler, but I would prefer to catch this event the way I catch "onselectednodechanged" for the TreeView.

Thanks in advance.

Best Answer

Here are some nice solutions on this link:

ASP.NET TreeView and Selecting the Selected Node

I like this solution, although I havent tried it:

TreeNode newCNode;

newCNode = new TreeNode("New Node");

newCNode.SelectAction = TreeNodeSelectAction.Select;

//now you can set the .NavigateUrl property to call the same page with some query string parameter to catch in the page_load()

newCNode.NavigateUrl = "~/ThisPage.aspx?args=" + someNodeAction

RootNode.ChildNodes.Add(newCNode);