首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新后的“最后刷新”消息

问更新后的“最后刷新”消息
EN

Stack Overflow用户
提问于 2013-11-13 17:30:33
回答 2查看 157关注 0票数 1

我似乎找不到我需要的东西,所以我会问。

我有一个页面,它将使用ASP:Updatepanel和timer_tick事件每5-10分钟自动更新一次。我只是在找一条信息,上面有这样的信息:

代码语言:javascript
复制
    Last refresh was at:
    <script>document.write(document.lastModified);</script>

或者类似的东西。有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-13 17:57:03

尝试使用ASP.NET服务器控件(即Label),该控件在服务器加载页面时得到更新,如下所示:

标记:

代码语言:javascript
复制
<asp:UpdatePanel>
    ...
    <asp:Label id="LabelLastUpdated" runat="server" />
</asp:UpdatePanel>

代码隐藏:

代码语言:javascript
复制
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
    ' Do update of data here and set last updated label to current time
    LabelLastUpdated.Text = "Last refresh was at: " & DateTime.Now.ToString("F")
End Sub

注意:"F"是完整的日期/时间模式(长时间)。有关更多信息,请阅读标准日期和时间格式字符串。

更新:

要使用这是母版页场景,任何内容页刷新都会导致标签更新,然后尝试如下:

母版页标记:

代码语言:javascript
复制
<html>
    <head>

    </head>
    <body>
        ... Existing content ...
        <asp:Label id="LabelLastUpdated" runat="server" />
    </body>
</html>

母版页的代码隐藏:

代码语言:javascript
复制
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
    ' Do update of data here and set last updated label to current time
    LabelLastUpdated.Text = "Last refresh was at: " & DateTime.Now.ToString("F")
End Sub

对于希望内容页通知母版页更新的母版页场景,请尝试如下:

母版页标记:

代码语言:javascript
复制
<html>
    <head>

    </head>
    <body>
        ... Existing content ...
        <asp:Label id="LabelLastUpdated" runat="server" />
    </body>
</html>

母版页的代码隐藏:

代码语言:javascript
复制
Sub UpdateLastUpdatedLabel()
    ' A content page is telling the last updated label to be set 
    ' to the current time
    LabelLastUpdated.Text = "Last refresh was at: " & DateTime.Now.ToString("F")
End Sub

在内容页的代码隐藏中:

在Page_Load或希望用作触发器更新母版页标签的任何其他事件中,请执行以下操作:

代码语言:javascript
复制
' Get a reference to the master page
Dim masterPage = DirectCast(Page.Master, YourMasterPageClassName)

' Now you can call the master page's UpdateLastUpdatedLabel method
' which will update the label's text to the current date/time
masterPage.UpdateLastUpdatedLabel()
票数 0
EN

Stack Overflow用户

发布于 2013-11-13 18:04:46

请尝试下面的代码,这些代码将定期更新。

在aspx页面中:

代码语言:javascript
复制
<asp:ScriptManager runat="server" id="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" id="UpdatePanel1">
  <ContentTemplate>
    <asp:Timer runat="server" id="Timer1" Interval="10000" OnTick="Timer1_Tick"></asp:Timer>
   <asp:Label runat="server" Text="Page not refreshed yet." id="LabelLastUpdated">
   </asp:Label>
  </ContentTemplate>
</asp:UpdatePanel>

在文件后面的代码中添加以下代码:

代码语言:javascript
复制
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        LabelLastUpdated.Text = "Last Modified at: " +DateTime.Now.ToLongTimeString();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19960460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档