如何在asp.net中正确地减少Ajax计时器?

时间:2014-06-20 10:16:44

标签: c# asp.net ajax timer

我正在进行在线考试项目,我必须在考试结束前显示倒计时。 我使用了以下代码:

aspx代码:

<asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTimer" style=" margin-top:35px; margin-left:825px;" runat="server"  Font-Bold="True" Font-Names="Arial" Font-Size="X-Large" ForeColor="#6600CC"></asp:Label>
<div style="margin-right:25px;">
<asp:AlwaysVisibleControlExtender ID="AlwaysVisibleControlExtender1"  runat="server" TargetControlID="lblTimer">
 </asp:AlwaysVisibleControlExtender>
<asp:Timer ID="timer1" runat="server"
Interval="1000" OnTick="timer1_tick"></asp:Timer>    
     </div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />
</Triggers>
</asp:UpdatePanel>

背后的代码是:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        bind();
        bind1();
        Result();
        Session["Result"] = lblshow.Text;

     if (!SM1.IsInAsyncPostBack)
    {
        SqlConnection sqlc1 = new SqlConnection(strcon);

        sqlc1.Open();

        String str = "select top 1 * from TestCreated order by Id desc";
        SqlCommand cmd1 = new SqlCommand(str, sqlc1);
        //sqlc.Open();
        SqlDataReader dr = cmd1.ExecuteReader();
        if (dr.Read())
        {
            Session["timeout1"] = dr["TestDuration"].ToString();

        }
        Session["timeout"] DateTime.Now.AddMinutes(Convert.ToInt32(Session["timeout1"].ToString())).ToString();
    }

    }    

}

protected void timer1_tick(object sender, EventArgs e)
{
    if (Session["timeout"] == null)
    Session["time"] = DateTime.Now.AddSeconds(10);
    if (0 > DateTime.Compare(DateTime.Now, DateTime.Parse(Session["timeout"].ToString())))
    {
        lblTimer.Text = string.Format("Time Left: 00:{0}:{1}",                     ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).TotalMinutes).ToString(), ((Int32)DateTime.Parse(Session["timeout"].ToString()).Subtract(DateTime.Now).Seconds).ToString());

    }

这段代码在本地服务器上工作正常但是当我在我的网络服务器上部署时,计时器没有正常倒计时,它会像这样减少4秒和5秒。甚至我试过通过改变我的计时器间隔但是它&# 39;不工作 如何让这个计时器正确倒计时?

2 个答案:

答案 0 :(得分:0)

// Add the ScriptManager and Timer Control.

<div>
<asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager>
<asp:Timer ID="timer1" runat="server" 
Interval="1000" OnTick="timer1_tick"></asp:Timer>
</div>
//   Add the update panel, 
//a label to show the time remaining and the AsyncPostBackTrigger.   
<div>
<asp:UpdatePanel id="updPnl" 
runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTimer" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName ="tick" />
</Triggers>
</asp:UpdatePanel>
</div>

这对你有用@Skoar ..

有关code project

的更多详情

答案 1 :(得分:0)

aspx代码是:

protected void timer1_tick(object sender, EventArgs e)
    {
        if (0 > DateTime.Compare(DateTime.Now, 
        DateTime.Parse(Session["timeout"].ToString())))
        {
            lblTimer.Text = "Number of Minutes Left: " + 
            ((Int32)DateTime.Parse(Session["timeout"].
            ToString()).Subtract(DateTime.Now).TotalMinutes).ToString();
        }
}