0
我正在開發一個Web應用程序,我需要一個倒數計時器。即時通訊使用asp.net在AJAX中的定時器問題asp.net
asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblHour" Text="" runat="server"></asp:Label>
<asp:Label ID="lblMin" Text="" runat="server"></asp:Label>
<asp:Label ID="lblSec" Text="" runat="server"></asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="timer_Tick">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
代碼背後
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["time"] = DateTime.Now.AddSeconds(40);
}
}
protected void timer_Tick(object sender, EventArgs e)
{
TimeSpan time1 = new TimeSpan();
time1 = (DateTime)Session["time"] - DateTime.Now;
if (time1.Seconds <= 0)
{
lblSec.Text = "TimeOut!";
}
else
{
lblSec.Text = time1.Seconds.ToString();
}
}
我有問題,計時器不會正確遞減。它從38開始,然後到35和32等等。
有沒有辦法解決這個問題?
我把定時器放在updatepanel之外,並使用觸發器來調用定時器功能。它解決了我的問題。不管怎麼說,還是要謝謝你 – Kanishka 2011-03-17 10:38:46