How to make Ajax timer decrement properly in asp.n

2019-06-02 19:11发布

I'm working on a online exam project in which I have to show a countdown till the exam ends. I have used the following code:

aspx code:

<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>

Code behind is:

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());

    }

This code is working fine on the local server but when I deployed it on my web server the timer is not counting down properly it decrements by 4 sec and 5 sec like this.even I tried by changing my timer interval but it's not working. How can I make this timer to countdown properly?

2条回答
看我几分像从前
2楼-- · 2019-06-02 19:38
// 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>

This will work for you @Skoar..

more details on code project

查看更多
Explosion°爆炸
3楼-- · 2019-06-02 19:58

aspx code is:

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();
        }
}         
查看更多
登录 后发表回答