问题与Jquery Ajax和页面加载会话分配
本文关键字:加载 会话 分配 Jquery Ajax 问题 | 更新日期: 2023-09-27 18:05:47
我有一个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;
}
上面的代码是工作的,直到我添加Session["UserName"]="abcd"在页面加载
Downloader.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Session["userName"] = "abcd!";
}
谁能告诉我发生了什么事?我已经花了2天时间想明白了,但还是不明白。
我猜你需要对会话进行条件化,写一些类似于post back或类似的东西。但我不确定……如果没有问题,你也可以试着把它移到页面预渲染。
对不起,我忘记了web表单。