Jquery Ajax和页面加载会话分配的问题?

时间:2011-08-16 09:53:29

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

我在asp.net中有一个后台工作者。我想传递进度中计入的百分比并将其显示在jquery进度条中。代码就像一个魅力, 显示进度条百分比... 0% - > 10% - > 20% - > .... - > 100%

但遗憾的是,当我在页面Load中分配Session [“UserName”] =“abc”时,它不会执行异步回发!进度条百分比显示为...... 0%---(直接在几秒后) - > 100%

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);
                      }
                  });
              }

Downloader.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;
    }

以上代码正常工作,直到我在页面加载中添加Session [“UserName”] =“abcd”

Downloader.aspx.cs

   protected void Page_Load(object sender, EventArgs e)
    {  
        Session["userName"] = "abcd!";
    }

任何人都可以告诉我这是什么?我已经花了2天时间来计算它但仍然没有想法

1 个答案:

答案 0 :(得分:0)

我猜你需要对会话进行条件化编写,如回帖或某种类型。但是肯定..如果这不是问题,你也可以尝试将它移动到预渲染页面。

我已经忘记了网络表格。