[英]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.我有一个更新面板,其中包含一个下拉列表,一旦页面完成加载,它将 go 并用最新数据填充下拉列表。 The data grab can take up to 3 minutes.
数据抓取最多可能需要 3 分钟。 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:
一旦页面完成渲染,我 javascript 在更新面板中调用 button.click 事件来获取数据:
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. object“myDataSource”是我创建的一个 object,它继承了 IEnumarable并有一个
public List<DictionaryEntry>
,这是“Key”和“Value”发挥作用的地方。
When the constructor is called, it goes to the webservice and fetches the data I want to use for the dropdown.调用构造函数时,它会转到 Web 服务并获取我要用于下拉列表的数据。 This fetch takes almost 3 minutes to complete, then stores it to my database.
此提取需要将近 3 分钟才能完成,然后将其存储到我的数据库中。 I then take the database table and populate the
public List<DictionaryEntry>
, then it is returned to the updatepanel for consumption.然后我获取数据库表并填充
public List<DictionaryEntry>
,然后将其返回到更新面板以供使用。
The way the architecture works for the fetch (its third party) is that I place a request (via web service) for the data object.该架构用于获取(其第三方)的方式是我为数据 object 发出请求(通过 web 服务)。 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.
然后我使用服务中的另一种方法并传入唯一 ID 来检查数据的状态。 I loop that check every 10 seconds.
我每 10 秒循环一次检查。 Once it returns a "complete" message, I use the same unique ID to fetch the actual data:
一旦它返回“完整”消息,我就使用相同的唯一 ID 来获取实际数据:
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.
我想我想在 while 循环中放一个 cog 来停止它。
Thanks for reading.谢谢阅读。
You can call .abortPostBack();
您可以调用
.abortPostBack();
to cancel the Ajax request取消 Ajax 请求
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有关详细信息,请查看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.您可以将
UpdateProgress
添加到您的页面和ProgressTemplate
put 内部,这将取消回发,您将有机会在代码中处理取消。 Also you should add the code from other answers to this button to cancel the post back.此外,您应该将其他答案中的代码添加到此按钮以取消回发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.