如何使阿贾克斯定时器递减正确的asp.net? [英] How to make Ajax timer decrement properly in asp.net?

查看:105
本文介绍了如何使阿贾克斯定时器递减正确的asp.net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作中,我必须展示一个倒计时,直到考试结束的在线考试项目。
我用下面的code:

ASPX code:

 < ASP:的ScriptManager ID =SM1=服务器>< / ASP:ScriptManager的>
< ASP:的UpdatePanel ID =updPnl=服务器的UpdateMode =条件>
<&的ContentTemplate GT;
< ASP:标签ID =lblTimer的风格=的margin-top:35px;保证金左:825px; =服务器FONT-粗体=真FONT-名称=宋体FONT-SIZE =X大前景色=#6600CC>< / ASP:标签>
< D​​IV的风格=保证金权:25px的;>
< ASP:AlwaysVisibleControlExtender ID =AlwaysVisibleControlExtender1=服务器的TargetControlID =lblTimer>
 < / ASP:AlwaysVisibleControlExtender>
< ASP:定时器ID =定时器1=服务器
时间间隔=1000OnTick =timer1_tick>< / ASP:定时器>
     < / DIV>
< /&的ContentTemplate GT;
<&触发器GT;
< ASP:AsyncPostBackTrigger控件ID =定时器事件名称=嘀/>
< /触发器>
< / ASP:的UpdatePanel>

code后面的是:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    如果(!Page.IsPostBack)
    {
        绑定();
        bind1();
        结果();
        会话[结果] = lblshow.Text;     如果(!SM1.IsInAsyncPostBack)
    {
        SqlConnection的sqlc1 =新的SqlConnection(STRCON);        sqlc1.Open();        字符串str =从TestCreated订单来自序号递减选择前1 *;
        CMD1的SqlCommand =新的SqlCommand(STR,sqlc1);
        //sqlc.Open();
        SqlDataReader的博士= cmd1.ExecuteReader();
        如果(dr.Read())
        {
            会话[timeout1] =博士[TestDuration]的ToString()。        }
        会话[超时] DateTime.Now.AddMinutes(Convert.ToInt32(会话[timeout1]的ToString()))的ToString()。
    }    }}保护无效timer1_tick(对象发件人,EventArgs的发送)
{
    如果(会话[超时] == NULL)
    会话[时间] = DateTime.Now.AddSeconds(10);
    如果(0方式> DateTime.Compare(DateTime.Now,DateTime.Parse(会话[超时]的ToString())))
    {
        lblTimer.Text =的String.Format(剩余时间:00:{0}:{1}, ((Int32)DateTime.Parse(Session[\"timeout\"].ToString()).Subtract(DateTime.Now).TotalMinutes).ToString(), (。(Int32)已DateTime.Parse(会话[超时]的ToString())减(DateTime.Now).Seconds)的ToString());    }

这code是工作的罚款在本地服务器上,但是当我部署它在我的Web服务器上的计时器没有倒计时得当,减了4秒,5秒,就像this.even我试图改变我的计时​​器间隔但它不工作。
我怎样才能让这个定时器倒计时正确?


解决方案

  //添加的ScriptManager和定时器控制。< D​​IV>
< ASP:的ScriptManager ID =SM1=服务器>< / ASP:ScriptManager的>
< ASP:定时器ID =定时器1=服务器
时间间隔=1000OnTick =timer1_tick>< / ASP:定时器>
< / DIV>
//添加更新面板,
//一个标签,以显示剩余时间和AsyncPostBackTrigger。
< D​​IV>
< ASP:的UpdatePanel ID =updPnl
=服务器的UpdateMode =条件>
<&的ContentTemplate GT;
< ASP:标签ID =lblTimer=服务器>< / ASP:标签>
< /&的ContentTemplate GT;
<&触发器GT;
< ASP:AsyncPostBackTrigger控件ID =定时器事件名称=嘀/>
< /触发器>
< / ASP:的UpdatePanel>
< / DIV>

这会为你工作.. @Skoar

上的 code项目

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?

解决方案

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

这篇关于如何使阿贾克斯定时器递减正确的asp.net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆