【问题标题】:How to pass the percentage of the progress in code behind to Jquery progress bar?如何将代码后面的进度百分比传递给Jquery进度条?
【发布时间】:2011-07-29 07:40:11
【问题描述】:

我在 asp.net 中有一个后台工作人员。我想传递正在更改的计数百分比并将其显示在 jquery 进度条中。不幸的是,我发现它只更新了一次进度条,并且只完成了进度。

jQuery

​​>
    $(document).ready(function() {
        $("#progressbar").progressbar();
              setTimeout(updateProgress, 100);
    });

   function updateProgress() {
              $.ajax({
                  type: "POST",
                  url: "Downloader.aspx/GetData",
                  data: "{}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  async: true,
                  success: function(msg) {
                      // Replace the div's content with the page method's return.

                      $("#progressbar").progressbar("option", "value", msg.d);
                  }
              });
          }

下载器.aspx.cs

static BackgroundWorker _bw;
public static int Percent { get; set; }

    protected void Button1_Click(object sender, EventArgs e)
    {
        _bw = new BackgroundWorker
        {
            WorkerReportsProgress = true,
            WorkerSupportsCancellation = true
        };
        _bw.DoWork += bw_DoWork;
        _bw.ProgressChanged += bw_ProgressChanged;
        _bw.RunWorkerCompleted += bw_RunWorkerCompleted;

        _bw.RunWorkerAsync("Hello world");
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {

        for (int i = 0; i <= 100; i += 20)
        {
            if (_bw.CancellationPending) { e.Cancel = true; return; }
            _bw.ReportProgress(i);
            Thread.Sleep(1000);
        }
        e.Result = 123;
    }

    void bw_RunWorkerCompleted(object sender,
                                       RunWorkerCompletedEventArgs e)
    {
            percentage.Text = "Complete: " + e.Result;      // from DoWork
    }

    void bw_ProgressChanged(object sender,
                                    ProgressChangedEventArgs e)
    {
        Percent = e.ProgressPercentage;
    }

    [WebMethod]
    public static int GetData()
    {
        return Percent;
    }

一旦 bw_ProgressChanged 检测到任何变化,如何更新 jquery 进度条?

【问题讨论】:

  • 问题是因为回发问题吗?

标签: c# jquery asp.net progress-bar


【解决方案1】:

您必须在 ajax 成功函数中递归您的 setTimeout(),直到该过程完成。

这种方法称为ajax-polling。

模拟 HTML:

    <asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
    Percentage:<asp:Label ID="percentage" runat="server"></asp:Label>

客户端脚本:

    $(document).ready(function () {
        // TODO: revert the line below in your actual code
        //$("#progressbar").progressbar();
        setTimeout(updateProgress, 100);
    });

    function updateProgress() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetData",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            success: function (msg) {
                // TODO: revert the line below in your actual code
                //$("#progressbar").progressbar("option", "value", msg.d);
                $("#percentage").text(msg.d);
                if (msg.d < 100) {
                    setTimeout(updateProgress, 100);
                }
            }
        });
    }

代码隐藏: 没有变化。

【讨论】:

  • 这不行,只有在后台工作完成后进度条才会更新。
  • @DEN,有什么错误吗? msg.d 的值是多少?您可以尝试删除parseInt 吗?还要检查您的 Percent 是否确实在服务器端递增。
  • 没有错误。是否删除 parseInt 没有区别。 msg.d一开始的值为0,进度完成后值为100。从0到100期间无法获取进度值:(是回发问题吗?还是直到进度才能获取值完成加载了吗?是否可以在进度运行时获取进度更改值?
  • 好的,我原来的帖子中有一个额外的),所以我删除了它。我也试过这是我的机器,它运行良好。 (我不知道您使用的是什么progressbar() 插件,所以在我的测试中我只使用了一个标签来显示进度。)我还假设您在您的aspx 页面中设置了Async="true"请检查已编辑的答案。
  • 嗨,亚历克斯,我找到了答案。解决方案是,我必须在 aspx 的更新面板中插入 Button1 控件。无论如何,我会将你的答案标记为:) 谢谢你的帮助!!
【解决方案2】:

你应该在 document.ready 中使用 setInterval(yourfunctionname,1000) insted of setTimeout。

  $(document).ready(function() {
        $("#progressbar").progressbar();
              setInterval(updateProgress, 100);
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 2017-03-24
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多