当计时器会话结束时,使窗体上的按钮不可见



我想在页面上的计时器用完时使按钮不可见。我页面上的计时器设置为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代码的完整代码:

<%@ 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秒。

最新更新