2012-07-24 78 views
2

我的应用程序中有一个要求显示日期和时间。 我在我的.net应用程序中使用Ajax来执行此操作。在ASP.net中显示当前时间并让它打勾

目前我有这样的代码

    <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <asp:Timer ID="UpdateTimer" runat="server" Interval="1000" OnTick="UpdateTimer_Tick" /> 
        <asp:UpdatePanel ID="TimedPanel" runat="server" UpdateMode="Conditional"> 
         <Triggers> 
          <asp:AsyncPostBackTrigger ControlID="UpdateTimer" EventName="Tick" /> 
         </Triggers> 
         <ContentTemplate> 
          <asp:Label runat="server" ID="DateStampLabel" /> 
         </ContentTemplate> 
        </asp:UpdatePanel> 

当你刷新页面它工作除了在一周秒钟后就好了。我的问题是,这是完成这个最好的方法吗?我也许可以摆脱不显示秒......这可能不是太重要。如果我这样做,我该怎么做?

这里是代码隐藏...

Protected Sub UpdateTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) 
    DateStampLabel.Text = DateTime.Now.ToString() 
End Sub 

谢谢!

+2

你想显示服务器时间或客户端的时间? (如果后者使用javascript only解决方案将是更好的选择) – Oded 2012-07-24 19:50:46

+1

或者,在返回的HTML中显示服务器的时间,但使用Javascript将该时间与本地机器进行比较,然后根据本地时钟每秒更新一次,所以它将显示服务器的时间而不需要轮询它。 – Dai 2012-07-24 19:52:36

+0

好问题。这真的没关系,因为这将在公司的Intranet上使用。我不确定...我猜客户可能是最好的... – envinyater 2012-07-24 19:55:06

回答

1

我不会建议你这样做,因为每次你想刷新计时器(每秒)时,你实际上正在点击服务器以显示计时器。很明显,这种方法的性能影响是负的

而且您还在使用邪恶UpdatePanel这意味着您每次都在执行整个页面生命周期。

如果你依赖客户端浏览器上你可以完成相同的行为:

$('#digital-clock').clock({offset: '0', type: 'digital'}); 
<ul id="digital-clock" class="digital"> 
    <li class="hour"></li> 
    <li class="min"></li> 
    <li class="sec"></li> 
    <li class="meridiem"></li> 
</ul> 

参考:

http://joaquinnunez.cl/jquery-clock-plugin/