我似乎无法找到我需要的东西所以我会问。
我有一个页面将使用ASP:Updatepanel和timer_tick事件每5-10分钟左右自动更新一次。我只是在寻找一条类似的信息:
Last refresh was at:
<script>document.write(document.lastModified);</script>
或类似的东西。 有什么建议吗?
答案 0 :(得分:0)
尝试使用在服务器执行页面加载时更新的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()
答案 1 :(得分:0)
请尝试按照定期更新的代码
在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();
}