I want to make a button invisible when the timer on my page is up. The timer on my page is set to be up in 1 minute. Once the timer is up, I want to set the button visible=false, but the button does not become invisible.
Below is the code for the timer:
<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: <asp:Label ID="Label2" runat="server"></asp:Label></span>
<br />
</ContentTemplate>
</asp:UpdatePanel>
below is the button on the page:
<asp:Button ID="btnTest" runat="server" Text="Test" OnClick="test_click" />
My aspx.cs code is like so:
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";
}
}
}
I am setting the btnTest.Visible = false; in my Timer_tick event when the time is up, but the btnTest does not become invisible. Below is the entire code for .cs code-behind page:
<%@ 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: <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>
Any help will be highly appreciated.