Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand .NET TreeView node by clicking its text instead of +/-

I've been using hardcoded hyperlinks for my web app navigation, but the app has grown since and managing it is becoming a real pain. I've decided to replace what I have with the TreeView control, however I want to make several changes to the way it looks.

Is there any property that needs to be set, that would allow user to expand the TreeView node by clicking its text instead of +/- ? I've already set ShowExpandColapse to 'false'.

I want my final result to end up as something similar to the TreeView on the left of the MSDN site.

Could anyone point me at the right direction please?

like image 518
Dr. Greenthumb Avatar asked Oct 26 '25 09:10

Dr. Greenthumb


2 Answers

Set TreeNode.SelectAction to either Expand, or SelectExpand.

like image 162
sisve Avatar answered Oct 29 '25 01:10

sisve


you can use xml data source or direct binding from db to treview

in the TreeView DataBound event we can write d recursive function as below to fetch each node and assign expand action to them.

 protected void TreeView1_DataBound(object sender, EventArgs e)
{

    foreach (TreeNode node in TreeView1.Nodes)
    {
        node.SelectAction = TreeNodeSelectAction.Expand;
        PrintNodesRecursive(node);
    }
}


    public void PrintNodesRecursive(TreeNode oParentNode)
    {


      // Start recursion on all subnodes.
     foreach(TreeNode oSubNode in oParentNode.ChildNodes)
  {
    oSubNode.SelectAction = TreeNodeSelectAction.Expand;
  PrintNodesRecursive(oSubNode);
  }
 }
like image 36
Shravan Ravi Avatar answered Oct 28 '25 23:10

Shravan Ravi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!