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

Noddy is in da house

2 Answers 62 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Stuart Hemming
Top achievements
Rank 2
Stuart Hemming asked on 20 May 2011, 09:08 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Vasil
Telerik team
answered on 20 May 2011, 04:50 PM
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.

0
Stuart Hemming
Top achievements
Rank 2
answered on 20 May 2011, 05:11 PM
Nice one Vasil,

Many thanks.

-- 
Noddy
Tags
Ajax
Asked by
Stuart Hemming
Top achievements
Rank 2
Answers by
Vasil
Telerik team
Stuart Hemming
Top achievements
Rank 2
Share this question
or