<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lbtxt" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lbtxt" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
</form>
protected void Page_Load(object sender, EventArgs e)
{
//异步回发
ScriptManager1.RegisterAsyncPostBackControl(this.Timer1);
}
protected void Timer1_Tick(object sender, EventArgs e)
{
//lbtxt.Text = DateTime.Now.ToString("hh:MM:ss");
lbtxt.Text = GenerateStringID();
}
private string GenerateStringID()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray())
{
i *= ((int)b + 1);
}
return string.Format("{0:x}", i - DateTime.Now.Ticks);
}
ASP.NET AJAX定时更新示例
本文介绍了一个使用ASP.NET AJAX技术实现定时更新页面内容的例子。通过设置Timer控件的Interval属性为1秒,并结合UpdatePanel组件,可以实现在不重新加载整个页面的情况下更新指定区域的内容。此示例中还演示了如何生成一个基于当前时间和GUID的独特字符串ID。

被折叠的 条评论
为什么被折叠?



