1

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

以下是代码

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,但计时器没有显示。

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

4

1 回答 1

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");
    }

于 2013-09-10T05:20:22.603 回答