$find(AjaxManager) returns null after second request

9 posts, 0 answers
  1. Johan Schoeman
    Johan Schoeman avatar
    2 posts
    Member since:
    Nov 2009

    Posted 04 Nov 2009 Link to this post

    I have a very simple test page that contains the following:
    RadAjaxManager
    RadScriptManager
    RadUpload
    RadWindowManager
    input type="button" x 2

    The input buttons fire client-side events that cause an ajaxRequest.
    This only works with the first ajaxRequest. Thereafter the  $find("<%= RadAjaxManager1.ClientID %>"); does not find the RadAjaxManager

    <head runat="server"
        <title></title
        <telerik:RadCodeBlock ID="cb1" runat="server"
     
            <script type="text/javascript"
     
                function InitiateAjaxRequest(arguments) { 
                    var ajaxManager = $find("<%= RadAjaxManager1.ClientID %>"); 
                    if (ajaxManager) ajaxManager.ajaxRequest(arguments); 
                }            
              
            </script> 
     
        </telerik:RadCodeBlock> 
    </head> 
    <body> 
        <form id="form1" runat="server"
        <div> 
     
            <script type="text/javascript"
     
                function OnClientFileSelected(radUpload, eventArgs) { 
                    var input = eventArgs.get_fileInputField(); 
                    var FileString = "UploadFile" + "|" + input.value; 
                    InitiateAjaxRequest(FileString); 
                } 
     
                function confirmCallBackFn(arg) { 
                    result = radalert("Confirm returned the following result: " + arg); 
                    InitiateAjaxRequest('confirm|' + result); 
                } 
     
            </script> 
     
            <div> 
                <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest"
                </telerik:RadAjaxManager> 
                <telerik:RadScriptManager ID="RadScriptManager1" runat="server"
                </telerik:RadScriptManager> 
            </div> 
            <div> 
                <telerik:RadUpload ID="RadUpload1" runat="server" Skin="Forest" OnClientFileSelected="OnClientFileSelected"
                </telerik:RadUpload> 
            </div> 
            <div> 
                <telerik:RadWindowManager ID="RadWindowManager1" runat="server"
                </telerik:RadWindowManager> 
            </div> 
            <div> 
                <input id="Button1" type="button" value="clientside button" onclick="javascript:InitiateAjaxRequest('btn|a');" /> 
            </div> 
            <div> 
                <input id="Button2" type="button" value="show CONFIRM BOX" onclick="radconfirm('Are you sure?', confirmCallBackFn); return false;" /> 
            </div> 
     
        </div> 
        </form> 
    </body> 
    I have tried the  setTimeout("InitiateAjaxRequest(FileString);", 0); trick but it does not make a difference.


  2. Pavlina
    Admin
    Pavlina avatar
    6187 posts

    Posted 05 Nov 2009 Link to this post

    Hi Johan,

    I have already responded in the support ticket you opened in connection to this subject. In order to avoid the unexpected behavior you should put RadScriptManager control before RadAjaxManager.
    To avoid duplicate posts, I will ask you to continue our communication there if you do not mind.

    Greetings,
    Pavlina
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. J. Chris Elgin
    J. Chris Elgin avatar
    1 posts
    Member since:
    Sep 2009

    Posted 13 Nov 2009 Link to this post

    I was experiencing similar problems, and rearranging the RadScriptManager and RadAjaxManager was no help.

    My solution was to grab it the first time and hang on to it.

    var myvar; 
    function RefreshPage(reason) 
        if (myvar == null
            myvar = $find('<%= RAP1.ClientID %>'); 
        myvar.ajaxRequest(reason); 

  4. Martin Roussel
    Martin Roussel avatar
    246 posts
    Member since:
    Jan 2010

    Posted 17 Oct 2012 Link to this post

    Hi,

    same situation than J. Chris Elgin (last post). I also partially solved it with his method but it only works when the two request are made in a specific order. When the order is Request A then Request B, it works, but if I refresh the page and start with Request B, it returns NULL. My real problem is that there is no strict order between my 2 requests (one can be occuring before the other depending on user actions).
    I have the RadScriptManager defined before the RadAjaxManager too.

    here is the code for Request A (always works):

    Page1.aspx.cs
    ScriptManager.RegisterStartupScript(this, Page.GetType(), "mykey", "CloseAndRebindComment();", true);

    Page1.aspx (note that this page is a radwindow popup....no RadAjaxManager defined)
    <body>
     
        <script type="text/javascript">
     
            function CloseAndRebindComment(args) {
                GetRadWindow().BrowserWindow.refreshCommentGrid(args);
                GetRadWindow().close();
            }

              function GetRadWindow() {
                var oWindow = null;
                if (window.radWindow) oWindow = window.radWindow; //Will work in Moz in all cases, including clasic dialog
                else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow; //IE (and Moz as well)

                return oWindow;
            }
     
       </script>
     <form id="form1" runat="server">
        <div class="OuterContainer">
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
    ...


    here is the code for Request B (only works when called after A):

    Page2.aspx.cs
    ScriptManager.RegisterStartupScript(this, Page.GetType(), "mykey", "refreshCommentGrid();", true);

    Page2.aspx (note that this page is parent to the radwindow popup)
    <body>
     
       
        <script type="text/javascript">
     
            var gblRadAjaxManager;
     
            function refreshCommentGrid(arg) {
     
                 if (gblRadAjaxManager == null) {
                     gblRadAjaxManager = $find("<%= RadAjaxManager1.ClientID %>");
                 }
     
                 gblRadAjaxManager.ajaxRequest("CommentRebind");
     
             }
     
          </script>
         <form id="form1" runat="server">
     
                    <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
     
                     <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="mngRequestStarted" OnAjaxRequest="RadAjaxManager1_AjaxRequest">
     
    ...



    Please help,

    Martin
  5. Martin Roussel
    Martin Roussel avatar
    246 posts
    Member since:
    Jan 2010

    Posted 22 Oct 2012 Link to this post

    Hi,

    ive managed to handle it by adding a setTimeout function into my javascript code:

    function refreshCommentGrid(arg) {
     
                 if (gblRadAjaxManager == null) {
     
                         setTimeout(function () {
                         gblRadAjaxManager = $find("<%= RadAjaxManager1.ClientID %>");
                        
                         if (gblRadAjaxManager != null) {
                            gblRadAjaxManager.ajaxRequest("CommentRebind");
                        }
     
                     }, 1000);
                 }
     
                 if (gblRadAjaxManager != null) {
                     gblRadAjaxManager.ajaxRequest("CommentRebind");
                 }
     
             }

    I took this solution from another forum post but wondering if this sounds normal behavior and if this is the best workaround for it since it doesnt work if I put a low timeout delay and think that I may get trouble if in some other cases the value should have been bigger.
  6. Gotcha
    Gotcha avatar
    64 posts
    Member since:
    Sep 2009

    Posted 30 Nov 2012 Link to this post

    Same problem... i had to use the setTimeout work around as well... 
    i recalled I had this problem before and putting the order of <form><RadScriptManager><RadAjaxmanager><RadCodeBlock> helped me then...

    This time, I was using an AjaxRequest which calls the server in turn call a RegisterStartupScript to call another Ajax Request ( so the 2nd time ) failed to see the Ajaxmanager... tried all the other solution, EnablePageHeader = false didnt work... Finally used this one.... although unorthodox... but it works. In fact the setTimeOut I've used 1 ms... instead of 1000ms ( to make it look instantaneous)

    Wonder why this is so... I'm also using a master Page with User COntrols ( dynamically generated )
  7. Gotcha
    Gotcha avatar
    64 posts
    Member since:
    Sep 2009

    Posted 04 Dec 2012 Link to this post

    From Support ... This may help others trying to look for a solution...

    In ASP.NET AJAX environment however, you should take into consideration the fact that the ASP.NET AJAX controls are loaded after window.onload is fired. You can verify this by putting some ASP.NET AJAX controls and then examine the HTML of the rendered page.

    Again, there are several ways to ensure that the controls are loaded on the client before trying to use them. If you want to use the RegisterStartupScript() method, I would recommend to check the ASP.NET AJAX’s Sys.Application.Load event. This event is raised after all scripts have been loaded on the page and the controls have been created and initialized. You need to make sure however, that the code that you will insert in the Load event will be executed only once and then removed, otherwise it will be called after every Ajax request. For example you could use the following logic:



    function f()  
    {  
        //code 
        Sys.Application.remove_load(f);  
    }  
    Sys.Application.add_load(f);
    
    
    I tried it on mine and it works as expected when nothing worked...
  8. Martin Roussel
    Martin Roussel avatar
    246 posts
    Member since:
    Jan 2010

    Posted 04 Dec 2012 Link to this post

    Gotcha,

    Im not sure If I get it right since it doesnt seem to solve it on my side. Do we need to put those 2 lines for every functions where setTimeout is needed? Im getting errors for the functions that contain parameters. Do you know how to do it for such functions?

    thanks for letting us know
  9. Gotcha
    Gotcha avatar
    64 posts
    Member since:
    Sep 2009

    Posted 04 Dec 2012 Link to this post

    Hi Martin

    Sorry... in my case my set up was an ajax call from a radbutton, which is executed in the server and which injects a javascript from code behind...

    I left my previous call below where the $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>");
    returns null;

    //string js = "refreshGrid('grdShoppingList_Rebind')"; // This will use the setTimeOut since $find(ajaxManager) will not be found

    // If you comment out the js below and uncomment the one above, you will see the problem 
    string js = "function f(){refreshGrid('grdShoppingList_Rebind'); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);";
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "RefreshGrid" + this.ClientID, js, true);


    This calls as you can see is a central javascript function refreshGrid() i have on my page....which refreshes a Grid. This function when called directly (without being called from the server jsinjection) works using the regular $find().... but when it is called from a js Injection, it wouldnt work... unless it's called like this with the add_load(), (remove_load() to avoid being called everytime)

    function refreshGrid(args) {
        var argArray = args.split("|");
        var ajaxManager = $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>");
        if (argArray[0] == 'wndShoppingListItem_Rebind') {
            ajaxManager.ajaxRequest("wndShoppingListItem_Rebind");
        } else if (argArray[0] == 'grdShoppingList_Rebind') {
            if (ajaxManager == null) {
                console.log('ajax null');
                setTimeout(function () {
                    ajaxManager = $find("<%= RadAjaxManager.GetCurrent(Page).ClientID %>");
                    if (ajaxManager != null) {
                        ajaxManager.ajaxRequest("grdShoppingList_Rebind");
                    }
                }, 1);
            } else {
            console.log('ajax not null');
                console.dir(ajaxManager);
                ajaxManager.ajaxRequest("grdShoppingList_Rebind");
            }
        }
    }
Back to Top