V.Prasad
V.Prasad

Reputation: 151

How to kill current process of page

I am using one asp.net application where i used master page and content page. In one of the content page i am loading one grid which contains more data, And in master page i used update panel to show progress bar image with cancel button. Now when i am loading the grid on content page then progress bar is showing with cancel button. For clearing progress i have written loader image to hide on cancel click button in javascript which is working fine.

But my process is still running How do i cancel this process on click on cancel button. Below is the method which triggered when click master page button.

private void ViewReports(string someFilterCondition)
{
   //Business logic to get data from db.
}

When this method called in business logic taking more time. When click on cancel button it hides the loading image but user don't know whether the process is cancelled or not and after sometime when he got the loaded gridview then knows that process was still running. How do i cancel the current process?

Upvotes: 1

Views: 688

Answers (1)

Kavin
Kavin

Reputation: 303

Declare the static sql command variable as global in that page (only command variable). Then write one web method and cancel the command in it. Then Call that web method using AJAX when click the cancel button.

//Add the namespace on top of the page

using System.Web.Services;

//declare this variable globally in that page

 static SqlCommand command = new SqlCommand();

//c#

[WebMethod]
public static string CancelProcess()
{
   try
   {
       command.Cancel();
       return "true";
   }
   catch (Exception ex)
   {
       return ex.Message.ToString();
   }
}

//.aspx

<asp:Button ID="BtnCancel" runat="server" Text="Cancel" OnClientClick="BtnCancel_ClientClick(); return false;" />

//JS

function BtnCancel_ClientClick() {
    $.ajax({
        type: "POST",
        url: "HomePage.aspx/CancelProcess",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
        },
        failure: function (response) {
            //  add error handling code here                   
        }
    });
}

Hope it helps you..

Upvotes: 1

Related Questions