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

Cancel Running Ajax Request

6 Answers 98 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 03 May 2015, 09:57 AM
Hello,

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

Sort by
0
Viktor Tachev
Telerik team
answered on 06 May 2015, 11:26 AM
Hello Daniel,

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.

 
0
Daniel
Top achievements
Rank 1
answered on 10 May 2015, 09:08 AM

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.

0
Viktor Tachev
Telerik team
answered on 13 May 2015, 08:33 AM
Hi 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Daniel
Top achievements
Rank 1
answered on 19 May 2015, 09:53 AM

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.

0
Viktor Tachev
Telerik team
answered on 21 May 2015, 12:44 PM
Hello 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Brett
Top achievements
Rank 1
answered on 27 May 2015, 02:42 PM
Daniel, Viktor's sample works fine to cancel a request.. regardless to whether it's a document or xhr request type.. 

You should consider handling whatever functionality you are performing in the canceling operation (that is taking 90 seconds) in a new thread. 
Tags
Ajax
Asked by
Daniel
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Daniel
Top achievements
Rank 1
Brett
Top achievements
Rank 1
Share this question
or