Noddy is in da house

3 posts, 1 answers
  1. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 20 May 2011 Link to this post

    Yes, it's time for another dumb questions from yours truly.

    I'm often surprised by how many times I find myself doing something for the first time, even though I've been doing this kind of work for a while now. Anyhow, here's the thing. I have a page with code on it that includes this ...

    <telerik:RadScriptBlock runat="server" ID="RadScriptBlock1" >
      <script type="text/javascript">
          $(document).ready(function ()
          {
              var addEditEventDialog = new CalendarAddEditEventObject();
          });
      </script>
    </telerik:RadScriptBlock>

    The constructor of the object being created includes code to attach a couple of client-side event handlers and it all works fine. Until, that is, my page does an Ajax callback. At this point my handlers stop working.

    Now, I've found a workaround but I'm unsure if it is the right work around. I've added RadScriptBlock1 in to the set of updated controls in the AjaxManager.

    What should I be doing?

    -- 
    Stuart
  2. Answer
    Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 20 May 2011 Link to this post

    Hi Stuart,

    If the calendar with the attached handlers gets updated by the Ajax it no longer has those handlers set, so you need to set them again.
    When you use $(document).ready your function will be executed at the earliest possible moment — as soon as the DOM is registered by the browser.

    However, after the Ajax request, this function is already executed, and not get executed second time.
    And here comes your workaround: When you ajaxify the ScriptBlock, you are basically running this code after each Ajax call, so you are registering the function again and again, and it gets executed.
    This works, however the code gets sent by the server to the client each time, which could be avoided. Here are two more workarounds, that don't require to update the ScriptBlock.

    JavaScript:
    function attachHandlers() {
      alert("attach Handlers");
    }
     
    window.onload = attachHandlers;
     
    function RequestEnd() {
      attachHandlers();
    }

    Aspx:
    <telerik:RadScriptBlock runat="server" ID="RadScriptBlock1">
      <script type="text/javascript">
        function attachHandlers() {
          alert("attach Handlers");
        }
        $(document).ready(attachHandlers);
     
        function RequestEnd() {
          $(document).ready(attachHandlers);
        }           
     
    </script>
    </telerik:RadScriptBlock>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnResponseEnd="RequestEnd">

    Greetings,
    Vasil
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 20 May 2011 Link to this post

    Nice one Vasil,

    Many thanks.

    -- 
    Noddy
Back to Top