当计时器会话启动时,使表单上的按钮不可见

making a button invisible on a form when the timer session is up

我想在页面上的计时器启动时隐藏一个按钮。我页面上的计时器设置为 1 分钟后启动。定时器一到,我想设置按钮visible=false,但是按钮并没有变得不可见。

下面是定时器的代码:

<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="1000" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" />
    </Triggers>
    <ContentTemplate>

        <div class="sticky">
            <span style="border: 2px; color: red; font-size: 25px;">Time Remaining: &nbsp;<asp:Label ID="Label2" runat="server"></asp:Label></span>
            <br />
    </ContentTemplate>
</asp:UpdatePanel>

下面是页面上的按钮:

 <asp:Button ID="btnTest" runat="server" Text="Test" OnClick="test_click" />

我的aspx.cs代码是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SendBulkEmail
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["CountdownTimer"] == null)
                {
                    Session["CountdownTimer"] = new TimeSpan(0, 1, 0);
                }
                TimeSpan current = (TimeSpan)Session["CountdownTimer"];
                Label1.Text = current.ToString("%m") + " minutes and " + current.ToString("%s") + " seconds";
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan ts2sec = new TimeSpan(0, 0, 2); // 2 seconds

            TimeSpan ts = (TimeSpan)Session["CountdownTimer"]; // current remaining time from Session
            TimeSpan current = ts - ts2sec; // Subtract 5 seconds
            Label1.Text = current.ToString("%m") + " minutes and " + current.ToString("%s") + " seconds";
            Session["CountdownTimer"] = current;  // put new remaining time in Session 

            if (current.Seconds == 0 && current.Minutes == 0)
            {
                Session["CountdownTimer"] = "";
                Timer1.Enabled = false;

                Label1.Text = "Your Session is timed out. ";

                btnTest.Visible = false;
            }

        }

        protected void test_click(object sender, EventArgs e)
        {
            string a = "This is Test";
        }
    }
}

我在我的 Timer_tick 事件中设置了 btnTest.Visible = false; 时间到了,但是 btnTest 并没有变得不可见。以下是 .cs code-behind 页面的完整代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="SendBulkEmail.WebForm4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="src1" runat="server"></asp:ScriptManager>
        <div>
            <asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="1000" />

            <asp:UpdatePanel ID="StockPricePanel" runat="server" UpdateMode="Conditional">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Timer1" />
                </Triggers>
                <ContentTemplate>

                    <div class="sticky">
                        <span style="border: 2px; color: red; font-size: 25px;">Time Remaining: &nbsp;<asp:Label ID="Label1" runat="server"></asp:Label></span>
                        <br />
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>

        <div>
            <asp:Button ID="btnTest" runat="server" Text="Test" OnClick="test_click" />

        </div>
    </form>
</body>
</html>

非常感谢任何帮助。

您的按钮不在 UpdatePanel 中 - 您的代码正在设置 btnTest.Visible = false,但未更新页面。当前页面状态变为无效(单击按钮将失败),但没有发生可见更新。 将你的按钮代码移入UpdatePanel,如下:

<asp:UpdatePanel ID="StockPricePanel" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" />
    </Triggers>
    <ContentTemplate>
        <div class="sticky">
            <span style="border: 2px; color: red; font-size: 25px;">Time Remaining: &nbsp;<asp:Label ID="Label1" runat="server"></asp:Label></span>
            <br />
            <div>
                <asp:Button ID="btnTest" runat="server" Text="Test" OnClick="test_click" />
            </div>
    </ContentTemplate>
</asp:UpdatePanel>

此外,由于 TimeSpan 可能变为负值,我建议更新您在 Timer1_Tick 事件处理程序中的检查,如下所示:

if (current.TotalMilliseconds <= 0)
{
    Session["CountdownTimer"] = "";
    Timer1.Enabled = false;

    Label1.Text = "Your Session is timed out. ";

    btnTest.Visible = false;
}

这将防止不能完全整除的值出现任何问题,例如一次减少 7 秒而不是 2 秒。