Can't find MS AJAX prototype object RRS feed

  • Question

  • User-1221966532 posted

    Hi guys,

    I've created a few web custom controls enhanced with some client capabilites (using MS AJAX prototype Sys.UI.Behavior). The web controls each implement the IScriptControl interface so that initializing the client AJAX control happens automatically by ASP.NET thanks to the GetScriptReferences() and GetScriptDescriptors() methods. Thes web controls sit inside an UpdatePanel and work like a charm. However a manually registered client script which tries to access the client prototype object using the $find() method returns NULL. The manual script is registered as

    ScriptManager.RegisterStartupScript(Page, typeof(ControlWithFullNameSpace), "key1", "CallMyMethod('ControlBehaviorID')", true);

    I know the Sys.Application scripts are added by ASP.NET to the end of the html form from what I read from the MSDN documentation. So my thinking is that the manual script tries to access the prototype object too early as the Sys.Application.add_init() didn't create the client objects yet. The ControlBehaviorID works properly as after loading the page an <button /> click script can actually find the prototype object.

    Did someone have similar issues?

    Monday, July 7, 2014 9:35 AM


  • User-1221966532 posted

    Actually just worked out what's being wrong. The manual script loads before the client prototype object is initialized so it can't see the client object. There are a couple of ways to fix this without using jQuery:

    1) <body onload="SharedFunctionToRegisterScripts"> - which onload attribute can be also set from code behind

    2) ScriptManager.RegisterStartupScript(Page, this.GetType(), "pageLoadScripts", "function pageLoad() { /*  inject script here */}", true); - also can store all the script strings in a list and register them on a single central code behind line of code. pageLoad() uses a trick well known in ASP.NET which I missed somehow...

    Hope this will help someone.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 7, 2014 1:22 PM