0
我在asp.net有一個後臺工作。我想通過更改進度計數的百分比,並將其顯示在jquery進度條中。代碼工作像一個魅力, 進度欄百分比顯示... 0% - > 10% - > 20% - > .... - > 100%問題與JQuery的Ajax和頁面加載會話分配?
但不幸的是,當我在頁面加載中分配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;
}
上面的代碼工作,直到我加入會話[「UserName」] =「abcd」在頁面加載
個Downloader.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Session["userName"] = "abcd!";
}
誰能告訴我這是怎麼回事?我已經花了2天的時間想出來,但仍然沒有任何想法T__T