ASP.NET异步更新标签 [英] ASP.NET Asynchronous label update

查看:85
本文介绍了ASP.NET异步更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行长的一个过程,我想作为过程进行一起更新页面上的标签,但我没有任何运气。

I have a process that runs long, and I want to update a label on the page as the process goes along, but I'm not having any luck.

这里的ASPX:

<%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Async.aspx.cs" Inherits="Website.structureDoc.Async" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="startAsyncButton" runat="server" Text="Run" onclick="startAsyncButton_Click"/>
        <asp:Button ID="cancelAsyncButton" runat="server" Text="Cancel" onclick="cancelAsyncButton_Click"/>

        <asp:label id="resultLabel" runat="server"></asp:label>

    </div>
    </form>
</body>
</html>

和这里的背后code:

And here's the code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Website.structureDoc
{
    public partial class Async : System.Web.UI.Page
    {
        BackgroundWorker backgroundWorker1;
        protected void Page_Load(object sender, EventArgs e)
        {
            backgroundWorker1 = new BackgroundWorker();

            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
        }

        protected void startAsyncButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy != true)
            {
                // Start the asynchronous operation.
                backgroundWorker1.RunWorkerAsync();
            }
        }

        protected void cancelAsyncButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.WorkerSupportsCancellation == true)
            {
                // Cancel the asynchronous operation.
                backgroundWorker1.CancelAsync();
            }
        }

        // This event handler is where the time-consuming work is done.
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 1; i <= 10; i++)
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    // Perform a time consuming operation and report progress.
                    System.Threading.Thread.Sleep(500);
                    worker.ReportProgress(i * 10);
                }
            }
        }

        // This event handler updates the progress.
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
        }

        // This event handler deals with the results of the background operation.
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            {
                resultLabel.Text = "Canceled!";
            }
            else if (e.Error != null)
            {
                resultLabel.Text = "Error: " + e.Error.Message;
            }
            else
            {
                resultLabel.Text = "Done!";
            }
        }
    }
}

都是后台的工人不是这个正确的方法?

Are Background Workers not the right approach for this?

如果可以使用AJAX我宁愿不这么做。

I'd rather not do this using AJAX if possible.

推荐答案

这是行不通的。问题是,asp.net作品上的请求/响应模型。为了接收的任何更新客户端(浏览器)需要请求来自服务器的信息。为了让您的标签进行更新,您将需要发送一个请求,其新的价值,后者又出来工作的价值是什么,用合适的值,为您展示响应服务器。

This is not going to work. The problem is that asp.net works on a request/response model. In order to receive any updates the client (browser) needs to request the information from the server. In order for your label to be updated you will need to send a request for its new value to the server which in turn works out what the value is and responds with the appropriate value for you to display.

最简单的方法是写这个使用Ajax和时序数据的要求,或替代设置一个计时器,并在页面刷新本身所设置的定时器的一些方法。底线是你需要的页面轮询更新的价值,只是因为你更新你的工作进程的价值并不意味着浏览器将收到此新的价值。

The easiest method would be to write this using AJAX with some way of timing the requests for data, or instead to set a timer and have the page refresh itself on a set timer. The bottom line is you need the page to poll for the updated value, just because you update the value in your worker process does not mean the browser will receive this new value.

我知道你说你没有想使用AJAX,但看看下面的 jQuery.get()为更好地理解你需要做的事情。

I know you said you didnt want to use AJAX but take a look at the following jQuery.get() to get a better understanding of what you need to do.

这篇关于ASP.NET异步更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆