Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# jQueryAjax和页面加载会话分配有问题吗?_C#_Jquery_Asp.net_Progress Bar - Fatal编程技术网

C# jQueryAjax和页面加载会话分配有问题吗?

C# jQueryAjax和页面加载会话分配有问题吗?,c#,jquery,asp.net,progress-bar,C#,Jquery,Asp.net,Progress Bar,我有一个asp.net的后台工作人员。我希望传递已更改进度中的百分比,并将其显示在jquery进度栏中。代码工作起来很有魅力, 进度条百分比显示为。。。0% --> 10% --> 20% --> .... -->100% 但不幸的是,当我在页面加载中分配Session[“UserName”]=“abc”时,它不会执行异步回发!进度条百分比显示为。。。0%--(几秒钟后直接)-->100% Jquery $(document).ready(function() { $("

我有一个asp.net的后台工作人员。我希望传递已更改进度中的百分比,并将其显示在jquery进度栏中。代码工作起来很有魅力, 进度条百分比显示为。。。0% --> 10% --> 20% --> .... -->100%

但不幸的是,当我在页面加载中分配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;
    }
   protected void Page_Load(object sender, EventArgs e)
    {  
        Session["userName"] = "abcd!";
    }

有人能告诉我发生了什么事吗?我已经花了两天的时间来解决这个问题,但仍然不知道怎么做。我猜你需要对会话进行条件性限制,比如写“如果发回”之类的东西。但是我不确定。。如果没有问题的话,你也可以尝试将其移动到页面预渲染

我忘记了网络表单