You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET页面后台进程真实进度展示问题求助

真实进度

所有关于<asp:UpdateProgress>的资料都只能提示用户等待,无法展示真实进度,因此我将这个问题命名为“真实进度”。我是Web开发新手,若有疏漏恳请指出。

我在WinForms程序中压缩文件时,通过System.IO.Compression的流操作获取字节数来更新进度条,压缩功能正常。现在想在WebForms中实现同样的真实进度展示,尝试了一种相对简单的方法:给UpdatePanel设置__doPostBack轮询间隔,让UpdatePanel的_Load事件读取后台压缩线程设置的Session变量。目前UpdatePanel的_Load能正常触发,但Session变量在该事件中始终为空。

我制作了一个包含UpdatePanel和Label的简单测试表单,同时也欢迎提供展示真实进度的更佳方案。

测试前台代码

<%@ Page Language="vb" MasterPageFile="~/MstrPg.Master" 
    AutoEventWireup="false" CodeBehind="TestForm.aspx.vb" 
    Inherits="TekntypeUpDownLoad.TestForm" %>

<asp:Content ID="CntntTest" runat="server" ContentPlaceHolderID="MainPg">
    <script type="text/javascript">
        window.onload = function () {
            setInterval("__doPostBack('<%=UpdatePanel1.ClientID%>', '');", 1000);
        }
    </script>
    <fieldset style="width:400px" class="transParent">
        <legend>Progress bar example</legend>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
            <ContentTemplate>
                <asp:Label ID="lblStatus" runat="server" Text="" ClientIDMode="Static"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
    </fieldset>
</asp:Content>

测试后台代码

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    'System.Threading.Thread.Sleep(4000)
    'lblStatus.Text = "Processing Completed"

    Dim ZipUpdThrd As New Threading.Thread(
        Sub()
            Session("CntrValue") = "Starting."
            For i = 1 To 5
                System.Threading.Thread.Sleep(1000) '模拟延迟
                'ScriptManager.RegisterStartupScript(Me, Me.GetType, "UpdProg" & i, "UpdProgress('lblStatus', '" & i & "');", True)
                Session("CntrValue") = i.ToString
                Debug.Print("i: " & i & " -- Session: " & Session("CntrValue"))
            Next
            Debug.Print("循环结束")
            Me.lblStatus.Text = "完成。"
        End Sub
            )
    ZipUpdThrd.Start()

    'ScriptManager.RegisterStartupScript(Me, Me.GetType, "UpdProg", "UpdProgress('lblStatus', 'Complete.');", True)
End Sub

Private Sub UpdatePanel1_Load(sender As Object, e As EventArgs) Handles UpdatePanel1.Load
    Debug.Print("panel load-" & Now & ": " & Session("CntrValue"))
    If Session("CntrValue") IsNot Nothing Then
        Me.lblStatus.Text = Session("CntrValue")
    Else
        Me.lblStatus.Text = "Session为空: " & Now
    End If
End Sub

内容的提问来源于stack exchange,提问作者Ken Krugh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 05:34:54