How to add a treenode as selectednode children in codebehind

  1. #1

    How to add a treenode as selectednode children in codebehind

    Hi,
    I need to add a treenode as selectednode children from database in the codebehind with a button clicked,but in directevent ,i can't get the selectednode which can append the child.Like this code
    var selectedNode = ((DefaultSelectionModel)treeCatalog.SelectionModel .Primary).SelectedNode

    i get the selectednode id,but there are no method to add child

    Did any one can help it?
  2. #2
    Hi,

    Please see the following sample
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !X.IsAjaxRequest)
            {
                this.BuildTree(TreePanel1.Root);
            }
        }
    
        private Ext.Net.TreeNodeCollection BuildTree(Ext.Net.TreeNodeCollection nodes)
        {
            if (nodes == null)
            {
                nodes = new Ext.Net.TreeNodeCollection();
            }
            
            Ext.Net.TreeNode root = new Ext.Net.TreeNode();
            root.Text = "Root";
            root.NodeID = "Root";
            nodes.Add(root);
    
            string prefix = DateTime.Now.Second + "_";
            for (int i = 0; i < 10; i++)
            {
                Ext.Net.TreeNode node = new Ext.Net.TreeNode();
                node.Text = prefix + i;
                root.Nodes.Add(node);
            }
    
            return nodes;
        }
    
        private static int ids = 0;
        protected void AddNodeClick(object sender, DirectEventArgs e)
        {
            TreePanel1.AppendChild("Root", new Ext.Net.TreeNode {
                NodeID = "Node_" + ++ids,
                Text = "New Node " + ids
            });
            TreePanel1.SelectNode("Node_" + ids);
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head2" runat="server">
        <title>Ext.NET Examples</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            
            <ext:TreePanel 
                ID="TreePanel1" 
                runat="server" 
                Icon="Anchor" 
                Title="Tree"
                AutoScroll="true" 
                Width="250" 
                Height="300"
                Collapsed="False" 
                CollapseFirst="True" 
                HideParent="False"
                RootVisible="False" 
                BodyStyle="padding-left:10px">
                
                <Buttons>
                    <ext:Button runat="server" Text="Add Node">
                        <DirectEvents>
                            <Click OnEvent="AddNodeClick" />
                        </DirectEvents>
                    </ext:Button>
                </Buttons>
             </ext:TreePanel>
        </form>
    </body>
    </html>
  3. #3
    Quote Originally Posted by Vladimir View Post
    Hi,

    Please see the following sample
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !X.IsAjaxRequest)
            {
                this.BuildTree(TreePanel1.Root);
            }
        }
    
        private Ext.Net.TreeNodeCollection BuildTree(Ext.Net.TreeNodeCollection nodes)
        {
            if (nodes == null)
            {
                nodes = new Ext.Net.TreeNodeCollection();
            }
            
            Ext.Net.TreeNode root = new Ext.Net.TreeNode();
            root.Text = "Root";
            root.NodeID = "Root";
            nodes.Add(root);
    
            string prefix = DateTime.Now.Second + "_";
            for (int i = 0; i < 10; i++)
            {
                Ext.Net.TreeNode node = new Ext.Net.TreeNode();
                node.Text = prefix + i;
                root.Nodes.Add(node);
            }
    
            return nodes;
        }
    
        private static int ids = 0;
        protected void AddNodeClick(object sender, DirectEventArgs e)
        {
            TreePanel1.AppendChild("Root", new Ext.Net.TreeNode {
                NodeID = "Node_" + ++ids,
                Text = "New Node " + ids
            });
            TreePanel1.SelectNode("Node_" + ids);
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head2" runat="server">
        <title>Ext.NET Examples</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            
            <ext:TreePanel 
                ID="TreePanel1" 
                runat="server" 
                Icon="Anchor" 
                Title="Tree"
                AutoScroll="true" 
                Width="250" 
                Height="300"
                Collapsed="False" 
                CollapseFirst="True" 
                HideParent="False"
                RootVisible="False" 
                BodyStyle="padding-left:10px">
                
                <Buttons>
                    <ext:Button runat="server" Text="Add Node">
                        <DirectEvents>
                            <Click OnEvent="AddNodeClick" />
                        </DirectEvents>
                    </ext:Button>
                </Buttons>
             </ext:TreePanel>
        </form>
    </body>
    </html>
    Thank you,that's ok now.
    But i have another problem about how to enum all node in a treepanel in codebehind?Because my root is a AsyncTreeNode,no method to get children about it.

Similar Threads

  1. Replies: 1
    Last Post: May 09, 2012, 8:31 AM
  2. [CLOSED] [1.0] How to select a treenode in codebehind?
    By klaus.schwarz in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 01, 2010, 3:00 PM
  3. [CLOSED] [1.0] TreePanel SelectedNode Issue
    By ljankowski in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Oct 05, 2010, 6:31 PM
  4. Replies: 0
    Last Post: May 20, 2010, 4:00 PM
  5. Replies: 0
    Last Post: Dec 10, 2009, 11:14 AM

Posting Permissions