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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Your button is not in the UpdatePanel – your code is setting btnTest.Visible = false, but is not updating the page. The current page state becomes invalid (clicking the button will fail), but no visible update happens. Move your button code into the UpdatePanel, as follows:
<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 />
<div>
<asp:Button ID="btnTest" runat="server" Text="Test" OnClick="test_click" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
Additionally, as TimeSpan can go negative, I recommend updating your check in Timer1_Tick event handler, as follows:
if (current.TotalMilliseconds <= 0)
{
Session["CountdownTimer"] = "";
Timer1.Enabled = false;
Label1.Text = "Your Session is timed out. ";
btnTest.Visible = false;
}
This will prevent any issues with not perfectly divisible values such as decrement of 7 seconds at a time instead of 2.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0