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

本文关键字:ajax 倒数 计时器 状态 asp net 维护 | 更新日期: 2023-09-27 18:30:54

我在互联网上创建了一个倒数计时器,由 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但计时器没有显示。

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

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

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