Cancel Running Ajax Request

7 posts, 0 answers
  1. Daniel
    Daniel avatar
    159 posts
    Member since:
    Feb 2014

    Posted 03 May 2015 Link to this post

    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.
  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 06 May 2015 Link to this post

    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.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Daniel
    Daniel avatar
    159 posts
    Member since:
    Feb 2014

    Posted 10 May 2015 in reply to Viktor Tachev Link to this post

    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.

  5. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 13 May 2015 Link to this post

    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
  6. Daniel
    Daniel avatar
    159 posts
    Member since:
    Feb 2014

    Posted 19 May 2015 in reply to Viktor Tachev Link to this post

    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.

  7. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 21 May 2015 Link to this post

    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
  8. Brett
    Brett avatar
    42 posts
    Member since:
    Aug 2014

    Posted 27 May 2015 in reply to Daniel Link to this post

    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. 
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017