开发者

Problem with Jquery Ajax and Page Load Session assigning?

开发者 https://www.devze.com 2023-03-28 14:57 出处:网络
I have a background worker in asp.net. I would like to pass the percentage counted in progress changed and display it in jquery progress bar. The code work like a charm,

I have a background worker in asp.net. I would like to pass the percentage counted in progress changed and display it in jquery progress bar. The code work like a charm, the progress bar percentage is showed... 0% --> 10% --> 20% --> .... -->100%

but unfortunately, when I assign Session["UserName"]="abc" in page Load , it doesn't execute Async postback! The progress bar percentage is showed ... 0% --- (directly after few seconds) --> 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;
    }

The above code is working until i added in Session["UserName"]="abcd" in page load

Downloader.aspx.cs

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

CAN ANYONE TELL ME WHAT's GOING ON? I ALREADY SPENT 2 DAYS TO FIGURE IT OUT BUT STILL HAVE NO IDEA T__T


I am guessing you need to conditionalise the session writing something like if post back or somethng of that sort. Nt sure though.. Can you also try moving thie to page pre render if that'd nt a problem.

Srry i hv forgotten web forms.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号