0

I have an asp.net page with a TreeView and a DropDownList. The TreeView is defined in the codebehind :

            <asp:TreeView ID="TreeViewResume" runat="server" ImageSet="Simple" NodeIndent="15" OnSelectedNodeChanged="ClickTreeViewResume">
            <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
            <NodeStyle Font-Names="Tahoma" Font-Size="12pt" ForeColor="#203239" HorizontalPadding="0px"
                NodeSpacing="0px" VerticalPadding="4px"></NodeStyle>
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="2px"
                VerticalPadding="0px" />
        </asp:TreeView>

and so the DropDownList :

<asp:Panel ID="PanelDdl" runat="server">Sélection de la période : <asp:DropDownList ID="Ddl" runat="server" OnSelectedIndexChanged="Ddl_SelectedIndexChanged" AutoPostBack="true">

I load the TreeView by code in the Page_Load() :

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        RemplirTableau();
    }
}

I would like that on the SelectedIndexChange() of the DropDownList the SQL source of the TreeView changes and the Treeview redraws.

protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    TreeViewResume.Nodes.Clear();
    RemplirTableau();        
}

Instead of this, only the first level nodes (parent nodes) of the TreeView are printed.

Any help please ?

messaid
  • 1
  • 1

2 Answers2

0

Calling TreeView.Nodes will only access the TreeView's direct children nodes. In order to access the lower children, you can use some type of recursion.

Something like:

void ClearTreeView(TreeNode parentNode){
  foreach(TreeNode childNode in parentNode.Nodes){
    if(childNode.Nodes.Count > 0){
      ClearTreeView(childNode);
    }
    else{
      childNode.Nodes.Clear();
    }
  }
}

This Thread might be helpful to you as it shows how to access all the children nodes, grandchildren, etc.

  • But using `TreeViewResume.Nodes.Clear()`clears the nodes... The problem I have is on the draw of the TreeView. When I debug after selecting a DropDownList item, I could see that every node is well created. But for a reason that I don't understand only the parent nodes are drawn. – messaid May 12 '20 at 14:22
  • Ah, my bad. I misunderstood the problem. Can you provide code for your RemplirTableau() function? – kirsten.madina May 12 '20 at 14:42
0

I simply needed to add a TreeViewResume.DataBind(); right after recreating the TreeView :

    protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    TreeViewResume.Nodes.Clear();
    RemplirTableau();
    TreeViewResume.DataBind();
}  
messaid
  • 1
  • 1