我似乎找不到我需要的东西,所以我会问。
我有一个页面,它将使用ASP:Updatepanel和timer_tick事件每5-10分钟自动更新一次。我只是在找一条信息,上面有这样的信息:
Last refresh was at:
<script>document.write(document.lastModified);</script>或者类似的东西。有什么建议吗?
发布于 2013-11-13 17:57:03
尝试使用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"是完整的日期/时间模式(长时间)。有关更多信息,请阅读标准日期和时间格式字符串。
更新:
要使用这是母版页场景,任何内容页刷新都会导致标签更新,然后尝试如下:
母版页标记:
<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()发布于 2013-11-13 18:04:46
请尝试下面的代码,这些代码将定期更新。
在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();
}https://stackoverflow.com/questions/19960460
复制相似问题