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

[Solved] Cannot get access to treeview in JS before the page finishes loading

2 Answers 219 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Jason Maronge
Top achievements
Rank 1
Jason Maronge asked on 30 Jan 2008, 10:58 PM
I am having a problem with accessing the treeview in javascript during the page load.  I need to do some work before my page finishes loading but it seems as though the treeview is one of the last things initialized on the page, thus I am not able to access it in JS.

I am doing the following:

ScriptManager.RegisterStartupScript(this.Page, typeof (string),"start",  
                                                string.Format(  
                                                    "mainTree = $find('{0}');window.ChangeDataContents(mainTree.get_selectedNode());",  
                                                    this.RadTreeView1.ClientID), true); 


But the html output shows this:

<script type="text/javascript">  
//<![CDATA[
mainTree = $find('ctl00_cph2_RadTreeView1');window.ChangeDataContents(mainTree.get_selectedNode());//]]> 
</script> 
<script type="text/javascript">  
//<![CDATA[  
Telerik.Web.UI.RadTreeView._preInitialize("ctl00_cph2_RadTreeView1","0");  
theFormtheForm.oldSubmit = theForm.submit;  
theForm.submit = WebForm_SaveScrollPositionSubmit

As you can see the treeview is not getting initialized until after I am trying to call it.  Is there a way around this?  Is there a way to give us a method that is called after the preinitalize so we can add our own code to? 

Jason

2 Answers, 1 is accepted

Sort by
0
Accepted
Simon
Telerik team
answered on 01 Feb 2008, 05:39 PM
Hi Jason Maronge,

This is because the script, which is registered on the server-side by the RegisterStartupScript, is output before the actual initialization of the control. Please try registering the script like this:


ScriptManager.RegisterStartupScript( 
            this.Page,  
            typeof(string),  
            "start"
            string.Format("Sys.Application.add_load(function(){{mainTree = $find('{0}');window.ChangeDataContents(mainTree.get_selectedNode());}});"
            this.RadTreeView1.ClientID),  
            true); 

This way, the custom function you want to execute during page load will successfully get the object.

Kind regards,
Simeon
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Jason Maronge
Top achievements
Rank 1
answered on 01 Feb 2008, 06:08 PM
Sweet.  Thanks a buch.

Jason
Tags
TreeView
Asked by
Jason Maronge
Top achievements
Rank 1
Answers by
Simon
Telerik team
Jason Maronge
Top achievements
Rank 1
Share this question
or