$find always returns null

10 posts, 0 answers
  1. Andrew Lawrence
    Andrew Lawrence avatar
    32 posts
    Member since:
    Mar 2007

    Posted 05 Mar 2009 Link to this post

    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.


  2. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 06 Mar 2009 Link to this post

    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.
  3. Andrew Lawrence
    Andrew Lawrence avatar
    32 posts
    Member since:
    Mar 2007

    Posted 10 Mar 2009 Link to this post

    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.
  4. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 10 Mar 2009 Link to this post

    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.
  5. Andy F.
    Andy F. avatar
    46 posts
    Member since:
    May 2006

    Posted 10 Mar 2009 Link to this post

    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).
  6. Andrew Lawrence
    Andrew Lawrence avatar
    32 posts
    Member since:
    Mar 2007

    Posted 10 Mar 2009 Link to this post

    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.
  7. Andy F.
    Andy F. avatar
    46 posts
    Member since:
    May 2006

    Posted 10 Mar 2009 Link to this post

    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.

     

  8. Jakub
    Jakub avatar
    3 posts
    Member since:
    Apr 2012

    Posted 14 Apr 2012 Link to this post

    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.
  9. Simon Vane
    Simon Vane avatar
    3 posts
    Member since:
    Jan 2004

    Posted 17 Apr 2013 Link to this post

    The link (http://www.telerik.com/help/aspnet-ajax/combo_clientsidebasics.html) does not work.  Could you correct it please?
    Thanks.
  10. Kate
    Admin
    Kate avatar
    1898 posts

    Posted 17 Apr 2013 Link to this post

    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.
Back to Top