Hide node in treeview control

I have a tree structure created on my HTML page

<asp:TreeView ID="TreeView1" runat="server" 
           onselectednodechanged="TreeView1_SelectedNodeChanged" 
           PopulateNodesFromClient="False" onunload="TreeView1_Unload">
           <Nodes>

               <asp:TreeNode Text="Reports" Value="Report">

               <asp:TreeNode Text="Status" Value="Service">
                   </asp:TreeNode>

                   <asp:TreeNode Text="Status" Value="Status">
                   </asp:TreeNode>

                   <asp:TreeNode Text="Stats" 
                       Value="Stats"></asp:TreeNode>

               </asp:TreeNode>
           </Nodes>
       </asp:TreeView>

now i want to hide the node statistics in the page load function in my code behind ....

any suggestions .. thanks

+3
source share
5 answers

I am using Telerik RadTreeView; TreeView has no DataBound and Visible events for each node. Here is the code to remove the node child for the TreeView.

protected void Page_Load(object sender, EventArgs e)
{
  RemoveNodeRecurrently(TreeView1.Nodes, "Status");
}

private void RemoveNodeRecurrently(TreeNodeCollection childNodeCollection, string text)
{
  foreach (TreeNode childNode in childNodeCollection)
  {
    if (childNode.ChildNodes.Count > 0)
      RemoveNodeRecurrently(childNode.ChildNodes, text);

    if (childNode.Text == text)
    {
      TreeNode parentNode = childNode.Parent;
      parentNode.ChildNodes.Remove(childNode);
      break;
    }
  }
}
+1
source

You can try this, it only works for leaf nodes.

TreeView1.Nodes[0].Text = "";

TreeView1.Nodes[0].ShowCheckBox = false;

PS: you will need a recursive function to access each node.

+1
source

node "", .

0

! [ This is how I used. ] [1]

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["type"] == null)
        {
            RemoveNodeRecurrently(rptTree.Nodes, "Create Users");
        }

        if (Session["user"] != null)
        {
        }
        else
        {
            Response.Redirect(ConfigurationManager.AppSettings.Get("RootFolder") + "/ERP - Login.aspx");
        }
    }

    private void RemoveNodeRecurrently(TreeNodeCollection childNodeCollection, string text)
    {
        foreach (TreeNode childNode in childNodeCollection)
        {
            if (childNode.ChildNodes.Count > 0)
                RemoveNodeRecurrently(childNode.ChildNodes, text);

            if (childNode.Text == text)
            {
                TreeNode parentNode = childNode.Parent;
                parentNode.ChildNodes.Remove(childNode);
                break;
            }
        }
    }
0
source
protected void Page_Load(object sender, EventArgs e)`{

TreeView1.Nodes.RemoveAt (2); } `

0
source

Source: https://habr.com/ru/post/1773130/


All Articles