我被难住了。当我的网站执行查询时,我正在尝试显示进度条。查询需要4-6分钟。我的进度条从数据库中获取它的值,Oracle有一个内置的查询将值提供给进度条。我使用的是EssentialObjects' ProgressBar。基本上,我只是将"Value“设置为1到100之间的整数。
以下是我的代码的简化版本:
页面:
<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>代码:
protected void btnExecute_Click(object sender, EventArgs e) {
tmr.Enabled = true;
ExecuteLongQuery();
}
protected void tmr_Tick(object sender, EventArgs e) {
pbr.Value = GetProgress();
}基本上,当我单击btnExecute时,计时器直到回发完成才会启动,从而使进度条永远不会显示。我尝试了一个回调,不确定我是否做对了,但在回发期间页面不显示结果。当页面处于异步回发状态时,如何让计时器(或任何内容)响应?
发布于 2011-07-21 20:50:35
直到回发完成后,才会将启用计时器这一事实传递给客户端。这就是它的工作方式。在服务器上执行代码不会立即对客户端产生影响。如果您在将响应发送到客户端之前等待ExecuteLongQuery()完成,那么您将永远看不到计时器。
最好的办法可能是在服务器上的单独线程中运行ExecuteLongQuery(),这样就可以完成回发并启动计时器。
我建议阅读ASP.Net页面生命周期- this看起来是一个很好的起点。
发布于 2011-12-16 15:23:27
我找到了这个,它对我很有效。您可以根据需要进行更改。它适用于每个页面回发,如果您想限制它,那么可以根据您的要求更改代码。
<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>https://stackoverflow.com/questions/6768686
复制相似问题