简体   繁体   English

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

[英]How do I update a page during an asynchronous postback?

I'm stumped.我难住了。 I am attempting to show a progress bar while my site executes a query.我试图在我的网站执行查询时显示进度条。 The query takes anywhere from 4-6 minutes.查询需要 4-6 分钟。 My progress bar gets its value from the database, Oracle has a built-in query to provide the values to the progress bar.我的进度条从数据库中获取它的值,Oracle 有一个内置查询来为进度条提供值。 I'm using EssentialObjects' ProgressBar .我正在使用EssentialObjects 的 ProgressBar Basically I just set "Value" to an integer between 1 and 100.基本上我只是将“值”设置为介于 1 和 100 之间的 integer。

Here is a simplified version of my code:这是我的代码的简化版本:

Page:页:

 <asp:UpdatePanel ID="upQuery" runat="server">
    <ContentTemplate>
        <asp:Button ID="btnExecute" runat="server" OnClick="btnExecute_Click" />
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upProgress" runat="server">
    <ContentTemplate>
        <asp:Timer ID="tmr" runat="server" Enabled="false" 
                   OnTick="tmr_Tick" Interval="3000"></asp:Timer>
        <eo:ProgressBar ID="pbr" runat="server" ></eo:ProgressBar>
    </ContentTemplate>
</asp:UpdatePanel>

Code:代码:

protected void btnExecute_Click(object sender, EventArgs e) {
        tmr.Enabled = true;
        ExecuteLongQuery();
}

protected void tmr_Tick(object sender, EventArgs e) {
        pbr.Value = GetProgress();
}

Basically when I click btnExecute, the timer doesn't start until the postback has completed, making the progress bar never show.基本上,当我单击 btnExecute 时,在回发完成之前计时器不会启动,从而使进度条永远不会显示。 I tried a callback, not sure if I did it right, but the page wouldn't show the result during postback.我尝试了回调,不确定我是否做对了,但页面在回发期间不会显示结果。 How do I get the timer (or anything) to respond while the page is in async postback?当页面处于异步回发状态时,如何让计时器(或任何东西)响应?

I found this and It works for me.我找到了这个,它对我有用。 You can change it according to your needs.您可以根据需要进行更改。 It works for every page postback, and if you want to restrict it, then change in code for your requirements.它适用于每个页面回发,如果您想限制它,请根据您的要求更改代码。

<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>

The fact that you've enabled the timer isn't passed to the client until the postback is completed.在回发完成之前,您启用计时器的事实不会传递给客户端。 That's just how it works.这就是它的工作原理。 Executing code on the server doesn't have an immediate effect on the client.在服务器上执行代码不会立即对客户端产生影响。 If you're waiting for ExecuteLongQuery() to complete before sending the response to the client then you'll never see a timer.如果您在将响应发送到客户端之前等待ExecuteLongQuery()完成,那么您将永远不会看到计时器。

Your best bet is probably to run ExecuteLongQuery() in a seperate thread on the server, allowing the postback to complete and the timer to start.您最好的选择可能是在服务器上的单独线程中运行ExecuteLongQuery() ,允许回发完成并启动计时器。

I would suggest reading up on the ASP.Net page lifecycle - this looks like a good place to start.我建议阅读 ASP.Net 页面生命周期——看起来是个不错的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从子页面触发更新面板回发? - How do I trigger an update panel postback from a child page? 在Silverlight中,如何处理异步调用期间的错误? - In Silverlight, how do I handle an error during an asynchronous call? 如何在异步数据库连接期间避免“访问冲突”? - How do I avoid “Access Violation” during asynchronous database connections? 如何检测我的页面是否是回发的结果 - How do I detect if my page is the result of a postback 我如何导致回发? - How do I cause a Postback? 如何在更新面板中进行部分回发期间存储值 - How to store a value during partial postback inside update panel 我如何回发到CGI服务 - How do I do a postback to a CGI service ASP.NET如何在回发期间发布更新(我知道回发是如何工作的,但仍然……) - ASP.NET how to post updates during postback (I know how the postback works but still…) 如何避免每次回发后页面跳动? - How do I keep my page from jumping around after every postback? 当用户从页面回发的下拉列表中选择任何项时,我想立即调用函数吗? - I want to call a function immediately as the user select any item from dropdown on page postback how to do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM