简体   繁体   English

从 DropDownList 值 C# ASP.NET 加载 TreeView

[英]Load a TreeView from a DropDownList value C# ASP.NET

I have an asp.net page with a TreeView and a DropDownList.我有一个 asp.net 页面,其中有一个 TreeView 和一个 DropDownList。 The TreeView is defined in the codebehind: TreeView 在代码隐藏中定义:

            <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:所以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():我通过 Page_Load() 中的代码加载 TreeView:

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.我希望在 DropDownList 的 SelectedIndexChange() 上 TreeView 的 SQL 源发生变化,并且 Treeview 重绘。

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.取而代之的是,仅打印 TreeView 的第一级节点(父节点)。

Any help please?请问有什么帮助吗?

Calling TreeView.Nodes will only access the TreeView's direct children nodes.调用 TreeView.Nodes 只会访问 TreeView 的直接子节点。 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.该线程可能对您有所帮助,因为它展示了如何访问所有子节点、孙子节点等。

I simply needed to add a TreeViewResume.DataBind();我只需要添加一个 TreeViewResume.DataBind(); right after recreating the TreeView:在重新创建 TreeView 之后:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM