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