This is a migrated thread and some comments may be shown as answers.

Tree View Problem

5 Answers 81 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Craig Broadhead
Top achievements
Rank 1
Craig Broadhead asked on 09 Dec 2011, 04:00 PM
Hi Im new to this telerik stuff and I have been dumped with a solution that is in a mess which i am trying to sort out so bear with me if im making any obvious telerik errors (please)!

I have a tree view that I am trying to populate client side, i understand that i can do this with the following code:

  var tree = $find("<%= uclPanelTabReport1.ClientID %>");
    tree.trackChanges();
    var node = new Telerik.Web.UI.RadTreeNode();
    node.set_text("New Node");
    tree.get_nodes().add(node);
    tree.commitChanges();

However i am having a couple of problems, the first it that the tree is null still after the find statement, I can get the correct value using the following  var tree = $("#RadTreeView1.ClientID %>"); but am unsure if this is a valid way of getting the reference.

When i have the reference to tree i am having an issue that on the next line     tree.trackChanges(); where I am being told by firebug that     tree.trackChanges() is not a functon. Can anyone point me to where I am going wrong? Im sure its something obvious but I cant see what!?!

5 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 09 Dec 2011, 07:09 PM
Hello Craig,

My first iquestion is if the ClientID you're passing to the $find method is actually the RadTreeView control's or some other control? From your example it looks like you might be passing in the wrong id. Also, when are you calling this code? If it's during the page load I would suggest using the following approach:

function pageLoad() {
    var tree = $find("<%= uclPanelTabReport1.ClientID %>");
    tree.trackChanges();
    var node = new Telerik.Web.UI.RadTreeNode();
    node.set_text("New Node");
    tree.get_nodes().add(node);
    tree.commitChanges();
}

The reason for calling it this during the pageLoad is because the RadTreeView client object isn't initialized during the normal js load event.

To answer you question about the $("#RadTreeView.ClientID") call, it does not retrieve the correct reference to the RadTreeView thus the reason FireBug would say trackChanges isn't a method of the RadTreeView.

I hope that helps.
0
Craig Broadhead
Top achievements
Rank 1
answered on 12 Dec 2011, 01:10 PM
Thanks thats cleared some items up for me, however im still stuggling. I am now getting a reference but by the sounds of it i am getting the wrong or an invalid one. It would seem that because im using an external js file i cant use the clientid method, and for some reason the $find("objectname"), where object name is the full id of the radtreeview is returning null, i suspect this to be because the radtreeview is injected into the page by a user control which is added after a button click on the page, this is done by a web service and thus without a postback, am i right in thinking this will be the problem? Is there a way round this? Is there a way to check to make sure the reference i have is the correct one?
0
Kevin
Top achievements
Rank 2
answered on 12 Dec 2011, 02:44 PM
Hello Craig,

If you don't know the ClientID and can't set it statically in your js file, you could use this code to find the RadTreeView:

function pageLoad() {
                for (var i = 0; i < $telerik.radControls.length; i++) {
                    if (typeof $telerik.radControls[i]._type != "undefined") {
                        if ($telerik.radControls[i]._type == "Telerik.Web.UI.RadTreeView") {
                            var radTreeView = $telerik.radControls[i];
                        }
                    }
                }
            }

So apparantly the $telerik static library holds an array of all the RadControls loaded on a page. The code above searches that array for a RadTreeView and grabs it's reference. Once you grab the reference you can do whatever you want to it. You will need to add some additional code to stop the loop or do any checks to ensure the RadTreeView found is the one you want to use, but this code should get you started.

I hope that helps.
0
Craig Broadhead
Top achievements
Rank 1
answered on 12 Dec 2011, 02:52 PM
Firstly kevin id like to say thanks for your help on this its been bugging me for days :).

secondly i tried using your loop and still failed to get a reference, interestingly $telerik.radControls.length when alerted shows 0, but i have a control there. any ideas?
   
0
Bozhidar
Telerik team
answered on 12 Dec 2011, 05:04 PM
Hello Craig Broadhead,

When you use a web service to load the user control to the page without a postback, the client object of the TreeView isn't created, so you won't be able to access it. If you want to take advantage of the client object, you have to load the usercontrol during a full or a partial postback.

Best wishes,
Bozhidar
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
TreeView
Asked by
Craig Broadhead
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
Craig Broadhead
Top achievements
Rank 1
Bozhidar
Telerik team
Share this question
or