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

$find always returns null

9 Answers 566 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Andrew Lawrence
Top achievements
Rank 1
Andrew Lawrence asked on 06 Mar 2009, 05:46 AM
Hi,
I have just updated from RadControls Classic. By and large the upgrade went smoothly but I am finding that a lot of my client side script is breaking.

Previously I could do something like this (just an example to demonstrate the point):
Page.ClientScript.RegisterStartupScript(GetType(),"key"
    myRadComboBox.ClientID + ".somefunction();"true); 

From the documentation it looks like this code should be rewritten like this:
Page.ClientScript.RegisterStartupScript(GetType(), "key"
    "$find(\"" + myRadComboBox.ClientID + "\").somefunction();"true); 

However, when I try this, the $find method returns null.

I notice that these scripts are being rendered above the Sys.Application.add_init calls for the control which could be part of the problem but I don't know how to solve this elegantly.


9 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 06 Mar 2009, 10:14 AM
Hello Andrew Lawrence,

Please see this help topic for the correct approach.

Regards,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Andrew Lawrence
Top achievements
Rank 1
answered on 10 Mar 2009, 06:57 AM
Hi,
The article you point to only deals with writing script using the declarative syntax, not for registering scripts in the code behind. Unfortunately it is not always possible or desirable to use declarative syntax. Is there a way to make it work using the Page.ClientScript.Register... methods?

I am pretty certain the problem is a timing issue with System.Application.add_init script and scripts registered through Page.ClientScript. For example, if I use setTimeout to delay 1 second before calling the $find function, I can access the combo box.
0
Veselin Vasilev
Telerik team
answered on 10 Mar 2009, 08:54 AM
Hi Andrew Lawrence,

In ASP.NET Ajax you need to use: ScriptManager.RegisterStartupScript method.

I hope this helps.

Greetings,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Andy F.
Top achievements
Rank 1
Iron
answered on 10 Mar 2009, 07:22 PM
Tried that.  Failed. 

What's happening is that the code rendered by the RadScriptManager.RegisterStartupScript is being attempted BEFORE the code to initial all the Telerik objects in the page.  What I believe we both seek is a way to force the code to be included in the page AFTER the Telerik objects are initialized (essentially on the line right above </form> instead of above all the Telerik object initialization code that comes right above the </form> tag).
0
Andrew Lawrence
Top achievements
Rank 1
answered on 10 Mar 2009, 10:21 PM
I agree, I don't think ScriptManager.RegisterStartupScript is the best solution.

This is really a nasty issue and it breaks a lot of scripts.

I asked Peter Blum about this issue over at his site and he came back with this reply:


I have had the same experience. The challenge
is that scripts written to the Microsoft RegisterStartupScript and
RegisterClientScriptBlock methods don't have a clearly determined order.
......
I solved it the same way you proposed: use setTimeout!



So there you have it, straight from the horse's mouth almost.

Hope this helps anyone else with this issue.
0
Andy F.
Top achievements
Rank 1
Iron
answered on 10 Mar 2009, 10:26 PM
I found another way that doesn't rely on timing:

Near the bottom, above the </form> line:

 

<script language="javascript" type="text/javascript">

 

Sys.Application.add_load(initializePage);

 

function initializePage() {

 

    // the code you want done
}

 

</script>

Not sure this works always, but it works for me so far.

 

0
Jakub
Top achievements
Rank 1
answered on 14 Apr 2012, 02:05 PM
Maybe a little bit late but I was bitten by the same problem. Andy F. solution unfortunatelly does not work with partial postbacks (load event handler survives partial postback). RadAjaxManager has ResponseScripts collection that solves this problem but only for partial postbacks.

This solution (used by ASP.NET AJAX Toolkit) seems to be bulletproof:

(function () {
    var fn = function () {
        var ajaxControl = $find('AJAX control');
        // do something usefull
    };

    Sys.Application.add_load(fn);
})();

For more information see this blog post.

Hopefully it helps somebody.
0
Simon Vane
Top achievements
Rank 1
answered on 17 Apr 2013, 05:59 AM
The link (http://www.telerik.com/help/aspnet-ajax/combo_clientsidebasics.html) does not work.  Could you correct it please?
Thanks.
0
Kate
Telerik team
answered on 17 Apr 2013, 06:10 AM
Hi Simon,

Below you can find the correct link:
http://www.telerik.com/help/aspnet-ajax/combobox-client-side-basics.html

Kind regards,
Kate
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
ComboBox
Asked by
Andrew Lawrence
Top achievements
Rank 1
Answers by
Simon
Telerik team
Andrew Lawrence
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Andy F.
Top achievements
Rank 1
Iron
Jakub
Top achievements
Rank 1
Simon Vane
Top achievements
Rank 1
Kate
Telerik team
Share this question
or