尝试使用在服务器加载页面时更新的 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()