开发者

Can I "cancel" a long-running process that is happening within an updatepanel?

I have an updatepanel that contains a dropdown that once the page has finished loading that will go and populate the dropdown with the latest data. The data grab can take up to 3 minutes. Can I opt for the user to "cancel" the request and just use the last version of the data?

Within the updatepanel, I have an unbound dropdown (at design time). Once the page finishes rendering, I javascript call a button.click event within the updatepanel to fetch the data:

private void RefreshDDL()
{
    hidAction.Value = "";
    ddlCampaigns.DataSource = myDataSource;
    ddlCampaigns.DataTextField = "Value";
    ddlCampaigns.DataValueField = "Key";
    ddlCampaigns.DataBind();
    ddlCampaigns.Visible = true;

    pnlDetails.Attributes.CssStyle.Clear();
    pnlPleaseWait.Visible = false;
    btnOK.Enabled = true;
}

The object "myDataSource" is an object I created that inherits IEnumarable and has an exposed public List<DictionaryEntry> which is where the "Key" and "Value" come into play.

When the constructor is called, it goes to the webservice and fetches the data I want to us开发者_运维知识库e for the dropdown. This fetch takes almost 3 minutes to complete, then stores it to my database. I then take the database table and populate the public List<DictionaryEntry>, then it is returned to the updatepanel for consumption.

The way the architecture works for the fetch (its third party) is that I place a request (via web service) for the data object. It immediately returns a unique identifier for the results. I then use another method from the service and pass in the unique ID to check the status of the data. I loop that check every 10 seconds. Once it returns a "complete" message, I use the same unique ID to fetch the actual data:

private void RefreshList()
{
    MyProxy proxyRequest = new MyProxy ();
    List<string> myList= new List<string>();
    if (UpdateNeeded())
    {
        ProgramManagement.ThirdPartyServiceApi.runReport report = new ProgramManagement.ThirdPartyServiceApi.runReport();
        report.reportName = "MyData";
        try
        {
            ProgramManagement.ThirdPartyServiceApi.runReportResponse response = proxyRequest.runReport(report);

            while (!ReportComplete(response.@return))
            {
                System.Threading.Thread.Sleep(10000);
            }
            StoreList(GetReport(response.@return));
        }
        catch (SoapException ex)
        {
            if (ex.Message == TOO_MANY_REQUESTS)
            {
                //display a message maybe?
            }
        }
    }

    AddDataToList();

}

Is there any way to stop this process mid-stream? I imagine I'd want to put a cog in the while loop to stop it.

Thanks for reading.


You can call .abortPostBack(); to cancel the Ajax request

function CancelPostBack() {

var objMan = Sys.WebForms.PageRequestManager.getInstance();

if (objMan.get_isInAsyncPostBack())

    objMan.abortPostBack();

}

For Details, take a look http://www.codedigest.com/Articles/ASPNETAJAX/125_Using_UpdateProgress_Control_Effectively.aspx


You can add an UpdateProgress to your page and inside of the ProgressTemplate put , and this will cancel the postback, and you will have the opportunity to handle the cancel in your code. Also you should add the code from other answers to this button to cancel the post back.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜