Building a navigation in asp.net using treeview control
NickName:rowkit Ask DateTime:2018-02-25T13:45:53

Building a navigation in asp.net using treeview control

I'm trying to build a windows explorer like simple navigation menu in asp.net using a Treeview control And I have placed a Listview control which displays the children of the selected node dynamically.

Root    
  -Group01
    -subgroup1    
      -1.1    
      -1.2   
    -subgroup2    
      -1.1
-Group02

On pageload I have fixed the Treenode to 0 so Group01 and Group02 buttons are inserted in the Listview.

What I want to know is suppose I click on the Group01 button in the Listview it should display subgroup1 and subgroup1 and so on instead of the rootnode's children.

This is the .aspx code

<div>
<asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
    <Nodes>
        <asp:TreeNode SelectAction="SelectExpand" Text="root" Value="root">
            <asp:TreeNode Expanded="False" SelectAction="SelectExpand" Text="group01" Value="group01">
                <asp:TreeNode Text="subgroup1" Value="subgroup1">
                    <asp:TreeNode Text="1.1" Value="1.1"></asp:TreeNode>
                    <asp:TreeNode Text="1.2" Value="1.2"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode Text="subgroup2" Value="subgroup2">
                    <asp:TreeNode Text="1.1" Value="1.1"></asp:TreeNode>
                </asp:TreeNode>
            </asp:TreeNode>
            <asp:TreeNode Expanded="False" SelectAction="SelectExpand" Text="group02" Value="group02">
            </asp:TreeNode>
       </asp:TreeNode>
    </Nodes>
</asp:TreeView>
</div>
 <div>
    <asp:ListView ID="ListView1" runat="server">
    </asp:ListView>
</div>

aspx.cs code

protected void Page_Load(object sender, EventArgs e)
{
    TreeView1.Nodes[0].Select();
    TreeView1_SelectedNodeChanged(this, EventArgs.Empty);
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
    ListView1.Controls.Clear();
    List<string> childs = new List<string>();
    for (int i = 0; i < TreeView1.SelectedNode.ChildNodes.Count; i++)
    {
        childs.Add(TreeView1.SelectedNode.ChildNodes[i].Text);
    }
    foreach (string val in childs)
    {
        Button btn = new Button
        {
            Text = val
        };
        ListView1.Controls.Add(btn);
        ListView1.Controls.Add(new LiteralControl("</br></br>"));
     }
}

Since I'm new to this I want to know whether this is the right way to do it or some alternatives.

Copyright Notice:Content Author:「rowkit」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48970603/building-a-navigation-in-asp-net-using-treeview-control

More about “Building a navigation in asp.net using treeview control” related questions

Building a navigation in asp.net using treeview control

I'm trying to build a windows explorer like simple navigation menu in asp.net using a Treeview control And I have placed a Listview control which displays the children of the selected node dynamic...

Show Detail

Suggestions for building a treeview control in Android

Suggestions for building a treeview control in Android

Show Detail

ASP.NET Treeview Control not expanding on click

I having an issue with the ASP.NET Treeview control. I create the treeview just fine but the nodes will not expand or collapse. I see there is a javascript error but it is for line 1 character 0 of...

Show Detail

Building a TreeView dynamically

I have to build a tree using the following data which comes from a DB: ID Name ManagerID 180002 john 180001 180003 Michel 180002 180005 smith 180003 john |_M...

Show Detail

Can I create a navigation control like this example in WPF using a TreeView?

I want to create a WPF control similar to the example below. Check out the link and look at the navigation control on the left. Can this be done in a treeview? If so, any idea how I would start? ...

Show Detail

Hiding ASP.NET SiteMap nodes in TreeView control

I have a SiteMap with All my nodes. I'm using a TreeView control which is linked to the SiteMap for navigation. Now I would like to hide certain nodes from appearing on the TreeView. Is it possible...

Show Detail

free editable treeview control in ASP.NET MVC

I want a free editable Treeview in ASP.NET MVC with context menu. Is any Treeview control available ?

Show Detail

Strange javascript error in ASP.NET TreeView control

I’m using Treeview Control (ASP.NET 2.0) in a web control. The funny thing is, that everything is working fine : expanding, collapse, selecting a node etc. ,but after every click firebug shows an e...

Show Detail

ASP.NET TreeView control trouble with allocation

I need some help from you: I develop some ASP.NET Web Form Application(.NET 4.5). There I have in my aspx file a treeview control declared. &lt;asp:TreeView ID="treeview" runat="server" ImageSet="

Show Detail

How to get Valuepath property of a asp.net treeview node using Jquery?

Can Anyone help me with the following queries How to get Valuepath property of a asp.net treeview node using Jquery? How to insert a node in asp.net treeview control using Jquery? How to delete a ...

Show Detail