在asp.net中维护基于ajax的倒数计时器的状态

时间:2013-09-10 05:02:38

标签: c# asp.net ajax timer

我已经在Code Project上使用会员4332221上的代码创建了一个倒数计时器,其中有一些更新,我在一个项目上使用这个计时器进行在线测试

以下是代码

aspx代码

<div>
<asp:ScriptManager ID= "SM1" runat="server"></asp:ScriptManager>
<asp:Timer ID="timer1" runat="server" 
Interval="1000" OnTick="timer1_tick"></asp:Timer>
</div>

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

aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!SM1.IsInAsyncPostBack)
        Session["timeout"] = DateTime.Now.AddMinutes(1).ToString();
}

protected void timer1_tick(object sender, EventArgs e)
{
    if (0 > DateTime.Compare(DateTime.Now,
    DateTime.Parse(Session["timeout"].ToString())))
    {
        int hrs = (((Int32)DateTime.Parse(Session["timeout"].
        ToString()).Subtract(DateTime.Now).TotalMinutes)) / 60;

        int mins = (((Int32)DateTime.Parse(Session["timeout"].
        ToString()).Subtract(DateTime.Now).TotalMinutes)) % 60;

        int seconds = (((Int32)DateTime.Parse(Session["timeout"].
        ToString()).Subtract(DateTime.Now).TotalSeconds))%60;


        lblTimer.Text = "Time left is " + hrs.ToString() + " : " + mins.ToString() + " : " + seconds.ToString();

        if (hrs == 0 && mins == 0 && seconds == 0)
        {
            lblTimer.Text = "Test Time Over";
        }
    }
}

如果我单独运行此代码工作正常,但问题是,如果我在我的模块中应用此代码,我想在按钮单击事件上启动它,它只能工作一次,同时点击任何其他按钮,计时器进入回到初始位置。

我尝试应用Postback = false,但计时器未显示。

如何在按钮单击时加载此计时器并在其他控件的单击事件上保持其状态

1 个答案:

答案 0 :(得分:1)

只需更改Page_Load事件即可...我只添加了一个条件(Session["timeout"] == null || Convert.ToString(Session["timeout"]).Trim() == "")

这是我的aspx代码


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="SM1" runat="server">
        </asp:ScriptManager>
        <asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick">
        </asp:Timer>
    </div>
    <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>
        <asp:Button ID="btn2" runat="server" Text="Clickkkk" onclick="btn2_Click" />
    </div>
    </form>
</body>
</html>

以下是我的.cs代码我,因为你可以看到我已经有按钮和值,点击按钮


  protected void Page_Load(object sender, EventArgs e)
    {
        if (!SM1.IsInAsyncPostBack && (Session["timeout"] == null || Convert.ToString(Session["timeout"]).Trim() == ""))
            Session["timeout"] = DateTime.Now.AddMinutes(3).ToString();
    }
    protected void timer1_tick(object sender, EventArgs e)
    {
        if (0 > DateTime.Compare(DateTime.Now,
        DateTime.Parse(Session["timeout"].ToString())))
        {
            int hrs = (((Int32)DateTime.Parse(Session["timeout"].
            ToString()).Subtract(DateTime.Now).TotalMinutes)) / 60;

            int mins = (((Int32)DateTime.Parse(Session["timeout"].
            ToString()).Subtract(DateTime.Now).TotalMinutes)) % 60;

            int seconds = (((Int32)DateTime.Parse(Session["timeout"].
            ToString()).Subtract(DateTime.Now).TotalSeconds)) % 60;


            lblTimer.Text = "Time left is " + hrs.ToString() + " : " + mins.ToString() + " : " + seconds.ToString();

            if (hrs == 0 && mins == 0 && seconds == 0)
            {
                lblTimer.Text = "Test Time Over";
            }
        }
    }

    protected void btn2_Click(object sender, EventArgs e)
    {
        Response.Write("asdasdsa");
    }