C# jQueryAjax和页面加载会话分配有问题吗?
我有一个asp.net的后台工作人员。我希望传递已更改进度中的百分比,并将其显示在jquery进度栏中。代码工作起来很有魅力, 进度条百分比显示为。。。0% --> 10% --> 20% --> .... -->100% 但不幸的是,当我在页面加载中分配Session[“UserName”]=“abc”时,它不会执行异步回发!进度条百分比显示为。。。0%--(几秒钟后直接)-->100% JqueryC# 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() { $("
$(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!";
}
有人能告诉我发生了什么事吗?我已经花了两天的时间来解决这个问题,但仍然不知道怎么做。我猜你需要对会话进行条件性限制,比如写“如果发回”之类的东西。但是我不确定。。如果没有问题的话,你也可以尝试将其移动到页面预渲染 我忘记了网络表单