后台如何使用两个定时器控制前台页面一个显示时钟,一个刷新表格数据(页面不刷新显示时钟和表格数据)

yct0605 2019-01-14 10:52:45
在网上找了一个方法:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="A1_1.aspx.cs" Inherits="PBT_A1_1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate"/>
<meta http-equiv="expires" content="0"/>
<title>A1_1</title>
</head>
<body style="background-image: url('../images/pagebody.jpg')">
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="div_pbt" runat="server" style="width:100%; height:100%; top: 0px; left:0px; position: fixed; background-image: url('../images/pbt.jpg');" visible="True">
<table id="tb_pbt" style="width:100%; font-family: 微软雅黑; font-size: 90px; color:#ffffff;" border="0">
<tr style="height:90px">
<td style="width:25px"></td>
<td style="width:930px"></td>
<td style="width:20px"></td>
<td colspan="3" style="width:930px"></td>
<td style="width:15px"></td>
</tr>
<tr style="height:60px;font-size:45px">
<td style="width:25px"></td>
<td style="width:930px"></td>
<td style="width:20px"></td>
<td style="width:300px"></td>
<td style="width:560px">
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<table id="tb_Time" style="width:100%; font-family: 微软雅黑; " border="0">
<tr style="height:100%">
<td style="width:100%"> <span style="position:relative; margin:0 3px 0px 0;float:left;"></span><asp:Label ID="Lb_Time" runat="server" ForeColor="Black"></asp:Label></td>
</tr>
</table>
<asp:Timer ID="Timer2" runat="server" Interval="100" OnTick="Timer2_Tick" Enabled="False"></asp:Timer>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer2" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
</div>
</td>
<td style="width:70px"></td>
<td style="width:15px"></td>
</tr>
<tr style="height:40px">
<td style="width:25px"></td>
<td style="width:930px"></td>
<td style="width:20px"></td>
<td colspan="3" style="width:930px"></td>
<td style="width:15px"></td>
</tr>
<tr style="height:150px">
<td style="width:25px"></td>
<td style="width:930px">2号</td>
<td style="width:20px"></td>
<td colspan="3" style="width:930px">1号</td>
<td style="width:15px"></td>
</tr>
<tr style="height:20px">
<td style="width:25px"></td>
<td style="width:930px"></td>
<td style="width:20px"></td>
<td colspan="3" style="width:930px"></td>
<td style="width:15px"></td>
</tr>
<tr style="height:150px">
<td style="width:25px"></td>
<td style="width:930px">产地:<asp:Label ID="Lb_cd2" runat="server" Text=""></asp:Label></td>
<td style="width:20px"></td>
<td colspan="3" style="width:930px">产地:<asp:Label ID="Lb_cd1" runat="server" Text=""></asp:Label></td>
<td style="width:15px"></td>
</tr>
<tr style="height:25px">
<td style="width:25px"></td>
<td style="width:930px"></td>
<td style="width:20px"></td>
<td colspan="3" style="width:930px"></td>
<td style="width:15px"></td>
</tr>
</table>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="100" OnTick="Timer1_Tick" Enabled="False"></asp:Timer>
</div>
</form>
</body>
</html>

//后台
{
if (!IsPostBack)
{
Timer1.Enabled = true;
Timer2.Enabled = true;
}
}

protected void Timer1_Tick(object sender, EventArgs e)
{
Timer1.Enabled = false;

ReadPBTInfo();
Timer1.Interval = PageTime_Norm;
Timer1.Enabled = true;
}

protected void Timer2_Tick(object sender, EventArgs e)
{
Timer2.Enabled = false;

Lb_Time.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

Timer2.Interval = 1000;
Timer2.Enabled = true;
}


刚开始可以正常运行,时钟每秒更新一次,数据正常更新,页面不刷新显示,运行两天后出现时钟不更新,数据可以正常更新,网上查了一下说一个页面内只能有一个timer正常运行,不知道我这个当时为什么可以显示,不知道有什么好的方法没有,时钟要求每秒实时更新的。
...全文
112 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hello World, 2019-01-14
  • 打赏
  • 举报
回复
前端用setInterval定时刷新就好了
  • 打赏
  • 举报
回复
可能是 Timer1 更新执行过程太卡,结果 Timer2 被大量请求阻塞出错了。你可以在 Timer1_Tick 中一开始也关闭 Timer2 的定时器,避免 Timer2 拥塞。 不过要想反应灵敏,就应该使用前端(浏览器端)编程。不要刷新页面。(UpdatePanel 也是一遍遍刷新页面的,后台页面消耗巨大)

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧