【问题标题】:"Last refresh" message after updatepanel更新面板后的“上次刷新”消息
【发布时间】:2013-11-14 03:53:11
【问题描述】:

我似乎无法准确找到我需要的东西,所以我会问。

我有一个页面将使用 ASP:Updatepanel 和 timer_tick 事件每隔 5-10 分钟左右自动更新一次。我只是在寻找类似以下内容的消息:

    Last refresh was at:
    <script>document.write(document.lastModified);</script>

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

【问题讨论】:

  • 你需要这个代码吗?

标签: javascript html asp.net vb.net


【解决方案1】:

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

标记:

<asp:UpdatePanel>
    ...
    <asp:Label id="LabelLastUpdated" runat="server" />
</asp:UpdatePanel>

代码隐藏:

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" 是完整的日期/时间模式(长时间)。阅读Standard Date and Time Format Strings了解更多信息。


更新:

要使用这是一个母版页场景,其中任何内容页面刷新都会导致标签更新,然后试试这个:

母版页的标记:

<html>
    <head>

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

母版页的代码隐藏:

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

对于您希望内容页面通知母版页更新的母版页方案,请尝试以下操作:

母版页的标记:

<html>
    <head>

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

母版页的代码隐藏:

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 或您希望用作更新母版页标签的触发器的任何其他事件中,执行以下操作:

' 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()

【讨论】:

  • 我觉得不是他想要的东西!
  • 差不多就是我要找的东西。我可以找到一些方法让它在我当前的页面上运行,但是如果我想加入,比如母版页,我该怎么做呢?
  • @bender54 - 将标签放在母版页标记中,并让母版页的 Page_Load 更新标签控件。如果任何内容页面导致回发,这将更新标签。我有一个在母版页中使用标签的单独示例,以及内容页面调用以实际更新标签的方法。查看更新的答案。
  • @bender54 - 如果可以,请随时对这个答案进行投票。 :-)
  • 我正要回来再次感谢母版页更新,它很漂亮。我会赞成..但我呃,没有代表。当我上去时,我会提醒自己这样做。
【解决方案2】:

请尝试以下会定期更新的代码

在 aspx 页面中:

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

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

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        LabelLastUpdated.Text = "Last Modified at: " +DateTime.Now.ToLongTimeString();
    }

【讨论】:

    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2012-07-09
    • 1970-01-01
    • 2014-03-09
    相关资源
    最近更新 更多