Dynamically Add TreePanel Node on codebehind

  1. #1

    Dynamically Add TreePanel Node on codebehind

    Hi,

    I see that at treepanel loader examples, loading operation is fired from client side.

    I have a treepanel and a FileUploadField which is in a form (https://examples1.ext.net/#/Form/FileUploadField/Basic/).
    When save button is pressed i change the selected node, and want to add new nodes to this selected node at code behind.

    I need to fire loading operation at server side.
    I do some db operations and if success, want to add a node to treepanel's selectednode.

    Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
    nodes.Add(new Ext.Net.TreeNode("p1", "picture Name", Icon.PictureLink));
    X.Js.AddScript("#{TreePanel1}.getSelectionModel().getSelectedNode().loadNodes(eval(\"(" + nodes.ToJson() + ")\"));");
    what is the problem with this code?Any idea? thanks
  2. #2
    Hi,

    Your code is almost fine :) Please look at the example.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void TestDirectEventHandler(object sender, DirectEventArgs e)
        {
            Ext.Net.TreeNodeCollection nodes = new Ext.Net.TreeNodeCollection();
            Ext.Net.TreeNode node = new Ext.Net.TreeNode("p1", "picture Name", Icon.PictureLink);
            node.Leaf = true;
            nodes.Add(node);
            X.Js.AddScript("#{TreePanel1}.getSelectionModel().getSelectedNode().loadNodes(eval(" + nodes.ToJson() + "));");
        }
    </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 runat="server">
        <title>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:TreePanel 
            ID="TreePanel1" 
            runat="server" 
            AutoHeight="true" 
            RootVisible="false">
            <Root>
                <ext:TreeNode Text="Root" Expanded="true">
                    <Nodes>
                        <ext:AsyncTreeNode Text="Node1" />
                        <ext:AsyncTreeNode Text="Node2" />
                    </Nodes>
                </ext:TreeNode>
            </Root>
        </ext:TreePanel>
        <ext:Button runat="server" Text="Test" OnDirectClick="TestDirectEventHandler" />
        </form>
    </body>
    </html>
  3. #3
    your sample seems non-mistaken beside mine :)

    the problem which i cannot give a meaning is

    auto generated script:
    ctl00_MainContent_TreePanel1.getSelectionModel().g etSelectedNode().loadNodes(eval([{id:"p53",leaf:true,iconCls:"icon-picturelink",text:"asdf"}]));

    error taken:
    Microsoft JScript runtime error: Object doesn't support this property or method
    it cannot find loadNodes.

    anyway.I have solved the problem by refreshing the treepanel and focusing on the node that just added.
  4. #4
    Quote Originally Posted by kutlu View Post
    auto generated script:
    ctl00_MainContent_TreePanel1.getSelectionModel().g etSelectedNode().loadNodes(eval([{id:"p53",leaf:true,iconCls:"icon-picturelink",text:"asdf"}]));

    error taken:
    Microsoft JScript runtime error: Object doesn't support this property or method
    it cannot find loadNodes.
    Well, I guess a selected node is not AsyncTreeNode. Only AsyncTreeNode has .loadNodes() method.
  5. #5
    Well, I guess a selected node is not AsyncTreeNode. Only AsyncTreeNode has .loadNodes() method.
    Thanks for your advice

Similar Threads

  1. How to get Node by id in treepanel 2.0?
    By tms2003@126.com in forum 2.x Help
    Replies: 1
    Last Post: Apr 03, 2012, 3:01 PM
  2. [CLOSED] Deleting node from treegrid dynamically
    By bakardi in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Oct 21, 2011, 1:53 PM
  3. Treepanel Node
    By lindgrenm in forum 1.x Help
    Replies: 0
    Last Post: Feb 10, 2010, 3:08 PM
  4. Replies: 0
    Last Post: May 27, 2009, 6:30 PM
  5. TreePanel:Copying from one node to another node using drag and drop
    By eighty20 in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Jan 25, 2009, 7:48 AM

Posting Permissions