我有一个过程,单击该按钮后,您将获得有关过程中位置的更新。 (处理11之1)
我尝试使用文本框和标签将文本保存在UpdatePanel中,但是随着过程的进行,我似乎都无法进行更新。
它没有更新。这是我的代码。
<div class="w3-row-padding w3-center w3-mobile">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="cmdSubmit" runat="server" Text="Create Life Budget" CssClass="w3-button w3-black" Font-Bold="False" UseSubmitBehavior="False" />
<br />
<br />
<asp:TextBox ID="txtProgress" runat="server" BackColor="Transparent" BorderStyle="None" Width="300px">In Progress ...</asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Protected Sub cmdSubmit_Click(sender As Object, e As EventArgs) Handles cmdSubmit.Click
divBudgetList.Visible = False
divCreateBudget.Visible = True
divReports.Visible = True
Master.HideMsg()
CreateLifeBudget()
End Sub
Public Sub CreateLifeBudget()
Dim iProgress As Long
Dim iProgressMax As Long = 11
iProgress = 1
txtProgress.Text = "Processing " & iProgress & " of " & iProgressMax
.... Other Code
iProgress = 1
txtProgress.Text = "Processing " & iProgress & " of " & iProgressMax
.... Other Code
txtProgress.Text = "Processing has completed"
End Sub
我尝试使用updatemode作为有条件的以及始终使用。 我还添加了触发器。
似乎没有任何作用。
任何帮助将不胜感激。
答案 0 :(得分:0)
以这种方式,您将无法获取状态更新,因为该方法将在任何事件返回给客户端之前在click事件中运行。
我知道做到这一点的唯一方法(这就是我的操作方式)是在单独的线程中运行CreateLifeBudget任务,然后立即返回客户端,启动一个调用页面方法或Ajax的客户端计时器调用以获取长期运行的任务的状态并从那里更新客户端。
答案 1 :(得分:0)
好的,我会尝试的,这是一个简化的解决方案,我只是根据我们在其他地方所做的事情而提出来的。
首先需要您的按钮和一些东西来显示进度(您实际上可以在按钮上显示进度,但这是语义):
<asp:UpdatePanel runat="server" ID="u" RenderMode="Inline" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button runat="server" ID="uxStartTaskBtn" Text="Start Task" CausesValidation="false" OnClick="uxStartTaskBtn_Click" />
<span id="progress"></span>
</ContentTemplate>
</asp:UpdatePanel>
然后,您需要使用一些JavaScript来进行进度检查:
<script type="text/javascript">
var longRunningTaskMonitorTimer = null;
function kickOffLongRunningTaskMonitor() {
longRunningTaskMonitorTimer = window.setInterval(function () {
window.PageMethods.getLongRunningTaskProgress(function (percentComplete) {
document.getElementById("progress").innerText = "Task is " + percentComplete + " percent complete";
if (percentComplete == 100) {
window.clearInterval(longRunningTaskMonitorTimer);
window.alert("Task Complete");
}
});
}, 800);
}
</script>``
单击该按钮时将调用此函数,并将反复轮询更新的进度,直到任务完成运行为止,没有错误检查,因此您需要将其放入,如果不希望无限期轮询,任务失败了!
在后端页面中,您将需要一个类来在线程中运行任务,还需要一个地方来存储对当前任务的引用,以及JS调用以获取进度的页面方法: '' 公共局部类LongRunningTaskPage:System.Web.UI.Page {
/// <summary>
/// Store a reference to the current task
/// </summary>
private static LongRunningTask CurrentTask {
get {
return (LongRunningTask)HttpContext.Current.Session["ct"];
}
set {
HttpContext.Current.Session["ct"] = value;
}
}
/// <summary>
/// Called when the start button is clicked, starts the task and sends back
/// some JS to the page to kick off the poller
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void uxStartTaskBtn_Click(object sender, EventArgs e) {
// Create and start the task
CurrentTask = new LongRunningTask();
CurrentTask.Start();
// This will kick off the javascript timer to check the progress of the long runnig task periodically
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "longRunningTask", "kickOffLongRunningTaskMonitor();", true);
}
/// <summary>
/// Method called from JS to get the progress of the current task
/// </summary>
/// <returns></returns>
[WebMethod]
public static int getLongRunningTaskProgress() {
return CurrentTask.CurrentProgress;
}
// This class represents a unit of work to do that takes a long time
private class LongRunningTask {
public int CurrentProgress { get; set; }
public void Start() {
var t = new Thread(() => DoWork());
t.Start();
}
public void DoWork() {
// This is where you do your work updating progress as applicable
for (var i = 1; i <= 100; i++) {
CurrentProgress = i;
Thread.Sleep(200); // Simlulate some long task
}
}
}
}''
代码非常简单,因为它没有错误处理,因此您需要添加它,有可能仅使用页面方法来完成全部操作,但是在我的情况下,我需要访问会话变量,因此必须这样做办法。我确信还有其他方法可以实现相同的目的,您不需要使用可以使用Ajax调用的页面方法,但这就是我们使用的方法。
这也是c#,我看到您的代码是VB,您应该足够容易地进行转置。
希望这会为您指明正确的方向。