I'm working on a project with controls that are updated by Ajax.
The project requirement is that the user may cancel a running request by clicking a button, and also fire the cancel event to the server.
How can I do this?
Thanks,
Daniel.
6 Answers, 1 is accepted
In order to cancel an AJAX request that is currently running you can fire a second request. This will cancel the first one.
Check out the following code snippets that illustrate the approach.
Markup:
<
telerik:RadAjaxManager
runat
=
"server"
ID
=
"RadAjaxManager1"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"Button1"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"Label1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
<
telerik:AjaxSetting
AjaxControlID
=
"Button2"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"Label1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
<
asp:Button
ID
=
"Button1"
Text
=
"click 1"
runat
=
"server"
OnClick
=
"Button1_Click"
/>
<
asp:Button
ID
=
"Button2"
Text
=
"click 2"
runat
=
"server"
OnClick
=
"Button2_Click"
/>
<
asp:Label
ID
=
"Label1"
Text
=
""
runat
=
"server"
/>
Code behind:
protected
void
Button1_Click(
object
sender, EventArgs e)
{
Thread.Sleep(2000);
Label1.Text =
"Updated by first button on "
+ DateTime.Now.ToString();
}
protected
void
Button2_Click(
object
sender, EventArgs e)
{
Thread.Sleep(1000);
Label1.Text =
"Updated by second button on "
+ DateTime.Now.ToString();
}
Regards,
Viktor Tachev
Telerik
See What's Next in App Development. Register for TelerikNEXT.

Hello Viktor,
Thank you for your answer,
Unfortunately, it does not work in our project!
My project is a report system. When a user clicks on a tree-node, request is sent to the server, a report is built and then its data fills the data grid.
If building the report takes a long time (for example: 10 minutes), and meanwhile the user presses the cancel button – STILL the user needs to WAIT ABOUT 90 SECONDS before the event cancel reaches the server!!
How can I fix this so that a click on the Cancel button will immediately fire to the server?
Thanks,
Daniel.
Have in mind that cancelling a request that is in progress can produce unexpected results in some scenarios. Generally, RadAjaxManager queues the requests and if a second request is initiated the first one will be disregarded. However, the server would still execute the requests and this can cause problems with the application.
Because of this it is not recommended to use such approach. I apologize for not explaining that in the first post.
Regards,
Viktor Tachev
Telerik

Hello Viktor,
Thank you for your answer.
our project requirement is that when the user presses the cancel button that request will get IMMEDIATELY to the server, and not queued. How can we achieve this?
Thanks,
Daniel.
I am afraid that this is not supported by the framework. This behavior is not related to Telerik controls.
When a request is sent it will be executed by the server. Moreover sending multiple requests in the described manner to the server could produce unexpected results.
Regards,
Viktor Tachev
Telerik

You should consider handling whatever functionality you are performing in the canceling operation (that is taking 90 seconds) in a new thread.