繁体   English   中英

在ASP.NET中维护基于Ajax的倒数计时器的状态

[英]Maintaining state of ajax based countdown timer in asp.net

我已经由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但是计时器未显示。

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

只需用以下..更改您的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");
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM