C# 如何在异步回发期间更新页面?

C# 如何在异步回发期间更新页面?,c#,asp.net,ajax,asynchronous,progress-bar,C#,Asp.net,Ajax,Asynchronous,Progress Bar,我被难住了。我试图在我的站点执行查询时显示进度条。查询需要4-6分钟。我的进度条从数据库中获取其值,Oracle有一个内置的查询来向进度条提供值。我正在使用。基本上我只是将值设置为1到100之间的整数 以下是我的代码的简化版本: 第页: 基本上,当我单击btnExecute时,计时器在回发完成之前不会启动,这使得进度条永远不会显示。我尝试了回调,不确定是否正确,但在回发过程中页面不会显示结果。当页面处于异步回发状态时,如何让计时器或任何东西响应?在回发完成之前,您已启用计时器的事实不会传递给客户

我被难住了。我试图在我的站点执行查询时显示进度条。查询需要4-6分钟。我的进度条从数据库中获取其值,Oracle有一个内置的查询来向进度条提供值。我正在使用。基本上我只是将值设置为1到100之间的整数

以下是我的代码的简化版本:

第页:


基本上,当我单击btnExecute时,计时器在回发完成之前不会启动,这使得进度条永远不会显示。我尝试了回调,不确定是否正确,但在回发过程中页面不会显示结果。当页面处于异步回发状态时,如何让计时器或任何东西响应?

在回发完成之前,您已启用计时器的事实不会传递给客户端。这就是它的工作原理。在服务器上执行代码不会立即影响客户端。如果在将响应发送到客户端之前等待ExecuteLongQuery完成,那么将永远看不到计时器

您最好的选择可能是在服务器上的单独线程中运行ExecuteLongQuery,从而完成回发并启动计时器


我建议阅读ASP.Net页面生命周期-看起来是一个很好的开始。

在回发完成之前,您已启用计时器的事实不会传递给客户端。这就是它的工作原理。在服务器上执行代码不会立即影响客户端。如果在将响应发送到客户端之前等待ExecuteLongQuery完成,那么将永远看不到计时器

您最好的选择可能是在服务器上的单独线程中运行ExecuteLongQuery,从而完成回发并启动计时器


我建议阅读ASP.Net页面生命周期-看起来是一个很好的起点。

我找到了这个,它对我很有用。您可以根据需要进行更改。它适用于每个页面的回发,如果您想限制它,请根据您的需求更改代码

<html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
        <style type="text/css">
             .modalPopup
             {
                 background-color: #696969;
                 filter: alpha(opacity=40);
                 opacity: 0.7;
                 xindex: -1;
             }
         </style>
     </head>
     <body>
         <form id="form2" runat="server">
              <asp:ScriptManager ID="ScriptManager2" runat="server" />
              <script type="text/javascript">
                  var prm = Sys.WebForms.PageRequestManager.getInstance();
                  //Raised before processing of an asynchronous postback starts and the postback request is sent to the server.
                  prm.add_beginRequest(BeginRequestHandler);
                  // Raised after an asynchronous postback is finished and control has been returned to the browser.
                  prm.add_endRequest(EndRequestHandler);
                  function BeginRequestHandler(sender, args) 
                  {
                      //Shows the modal popup - the update progress
                      var popup = $find('<%= modalPopup.ClientID %>');
                      if (popup != null) 
                      {
                          popup.show();
                      }
                  }

                  function EndRequestHandler(sender, args) 
                  {
                      //Hide the modal popup - the update progress
                      var popup = $find('<%= modalPopup.ClientID %>');
                      if (popup != null)  
                      {
                          popup.hide();
                      }
                  }
              </script>
              <div>
                  <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                      <ProgressTemplate>
                          <asp:Image ID="Image1" ImageUrl="waiting.gif" AlternateText="Processing" runat="server" />
                      </ProgressTemplate>
                  </asp:UpdateProgress>
                  <ajaxtoolkit:modalpopupextender id="modalPopup" runat="server" targetcontrolid="UpdateProgress" popupcontrolid="UpdateProgress" backgroundcssclass="modalPopup" />
                  <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                      <ContentTemplate>
                          <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                      </ContentTemplate>
                  </asp:UpdatePanel>
            </div>
        </form>
    </body>
</html>

我找到了这个,它对我有用。您可以根据需要进行更改。它适用于每个页面的回发,如果您想限制它,请根据您的需求更改代码

<html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
        <style type="text/css">
             .modalPopup
             {
                 background-color: #696969;
                 filter: alpha(opacity=40);
                 opacity: 0.7;
                 xindex: -1;
             }
         </style>
     </head>
     <body>
         <form id="form2" runat="server">
              <asp:ScriptManager ID="ScriptManager2" runat="server" />
              <script type="text/javascript">
                  var prm = Sys.WebForms.PageRequestManager.getInstance();
                  //Raised before processing of an asynchronous postback starts and the postback request is sent to the server.
                  prm.add_beginRequest(BeginRequestHandler);
                  // Raised after an asynchronous postback is finished and control has been returned to the browser.
                  prm.add_endRequest(EndRequestHandler);
                  function BeginRequestHandler(sender, args) 
                  {
                      //Shows the modal popup - the update progress
                      var popup = $find('<%= modalPopup.ClientID %>');
                      if (popup != null) 
                      {
                          popup.show();
                      }
                  }

                  function EndRequestHandler(sender, args) 
                  {
                      //Hide the modal popup - the update progress
                      var popup = $find('<%= modalPopup.ClientID %>');
                      if (popup != null)  
                      {
                          popup.hide();
                      }
                  }
              </script>
              <div>
                  <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                      <ProgressTemplate>
                          <asp:Image ID="Image1" ImageUrl="waiting.gif" AlternateText="Processing" runat="server" />
                      </ProgressTemplate>
                  </asp:UpdateProgress>
                  <ajaxtoolkit:modalpopupextender id="modalPopup" runat="server" targetcontrolid="UpdateProgress" popupcontrolid="UpdateProgress" backgroundcssclass="modalPopup" />
                  <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                      <ContentTemplate>
                          <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                      </ContentTemplate>
                  </asp:UpdatePanel>
            </div>
        </form>
    </body>
</html>

您应该提供有关线程和更多代码的更多信息。我们的想法是,您可以订阅查询/类的ProgressChanged事件,也可以在类内部调用用户界面,但为了提供帮助,我们需要查看更多代码。这是一个网页,而不是Windows窗体。没有ProgressChanged事件,因为我使用的是自定义控件。我不确定我还需要提供什么..ExecuteLongQuery的代码会很有帮助..您应该提供更多关于线程和代码的信息。我们的想法是,您可以订阅查询/类的ProgressChanged事件,也可以在类内部调用用户界面,但为了提供帮助,我们需要查看更多代码。这是一个网页,而不是Windows窗体。没有ProgressChanged事件,因为我使用的是自定义控件。我不确定我还需要提供什么..ExecuteLongQuery的代码会很有帮助..我想我的印象是使用UpdatePanel会启动另一个线程,但我想这并不容易。我来调查一下。你知道有什么好的教程吗?啊,没有。UpdatePanel只是做一个只重新加载部分页面的回发。在事件处理程序中的所有代码完成执行之前,它不会返回到客户端。我没有在.Net中执行线程的经验,但我相信您需要使用ASP.Net支持的辅助线程。谷歌搜索一下,你应该可以很容易地找到一个教程。祝你好运哇,那真的很容易。我刚用过教程。谢谢我想我的印象是使用UpdatePanel会启动另一个线程,但我想这并不容易。我来调查一下。你知道有什么好的教程吗?啊,没有。UpdatePanel只是做一个只重新加载部分页面的回发。在事件处理程序中的所有代码完成执行之前,它不会返回到客户端。我没有在.Net中执行线程的经验,但我相信您需要使用ASP.Net支持的辅助线程。谷歌搜索一下,你应该可以很容易地找到一个教程。祝你好运哇,那真的很容易。我刚用过教程。谢谢
<html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
        <style type="text/css">
             .modalPopup
             {
                 background-color: #696969;
                 filter: alpha(opacity=40);
                 opacity: 0.7;
                 xindex: -1;
             }
         </style>
     </head>
     <body>
         <form id="form2" runat="server">
              <asp:ScriptManager ID="ScriptManager2" runat="server" />
              <script type="text/javascript">
                  var prm = Sys.WebForms.PageRequestManager.getInstance();
                  //Raised before processing of an asynchronous postback starts and the postback request is sent to the server.
                  prm.add_beginRequest(BeginRequestHandler);
                  // Raised after an asynchronous postback is finished and control has been returned to the browser.
                  prm.add_endRequest(EndRequestHandler);
                  function BeginRequestHandler(sender, args) 
                  {
                      //Shows the modal popup - the update progress
                      var popup = $find('<%= modalPopup.ClientID %>');
                      if (popup != null) 
                      {
                          popup.show();
                      }
                  }

                  function EndRequestHandler(sender, args) 
                  {
                      //Hide the modal popup - the update progress
                      var popup = $find('<%= modalPopup.ClientID %>');
                      if (popup != null)  
                      {
                          popup.hide();
                      }
                  }
              </script>
              <div>
                  <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                      <ProgressTemplate>
                          <asp:Image ID="Image1" ImageUrl="waiting.gif" AlternateText="Processing" runat="server" />
                      </ProgressTemplate>
                  </asp:UpdateProgress>
                  <ajaxtoolkit:modalpopupextender id="modalPopup" runat="server" targetcontrolid="UpdateProgress" popupcontrolid="UpdateProgress" backgroundcssclass="modalPopup" />
                  <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                      <ContentTemplate>
                          <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                      </ContentTemplate>
                  </asp:UpdatePanel>
            </div>
        </form>
    </body>
</html>