I'm using a simple asp.net timer with this code as a backend
protected void Timer1_Tick(object sender, EventArgs e)
{
int seconds = int.Parse(Label1.Text);
if (seconds > 0)
Label1.Text = (seconds - 1).ToString();
else
Timer1.Enabled = false;
}
The timer is grouped along with a Label and a Button inside a UpdatePanel in my front-end.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Image ID="RedBox" runat="server" ImageUrl="~/images/redbox.png" Visible="false" CssClass="redbox1" ClientIDMode="Static" />
<asp:Button ID="schlbtn1" runat="server" Text="Go To Lectures" CssClass="btn btn-lg btn-success btn-block" OnClick="schlbtn1_Click" Visible="true" ForeColor="Black" ViewStateMode="Enabled" ClientIDMode="Static" />
<asp:Label ID="Label1" runat="server" Visible="false" CssClass="timerlbl" Font-Size="XX-Large">60</asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick" Enabled="false">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
I'm looking for a way to make the timer NOT to reset on every page refresh. I need the timer to still count down until it reach 0 even if the client is not on the same page. I tried Sleep methods but think this is not the solution. Please make any kind of suggestions.
Store the timeout in the session:
Add to page load:
if (Session["timer"] == null)
{
InitTimer(120); // or whatever initial value is
}
Add method:
public void InitTimer(int secs)
{
Session.Add("timer", secs);
Timer1.Enabled = true;
}
Change tick method:
protected void Timer1_Tick(object sender, EventArgs e)
{
int seconds = (int)Session("timer");
if (seconds > 0)
{
seconds--;
Session["timer"] = seconds;
Label1.Text = seconds.ToString();
}
else
Timer1.Enabled = false;
}
Related
Issue
I have a asp.net webForm that has several timers. When all the timers are active and running, the webform will "stop" running at exactly 82 seconds. There is no crash redirect or error, but if I look at the Dev Console it shows lots of 500 errors (screen shot below).
Other Information
The application has a total of 13 timers. 1 timer is just a clock, and ticks every 1 second (this is how I can see exactly when the page stops). The other 12 timers are panels that pull information on each tick. The ticks of these panels ranges from 5 minutes to 15 minutes.
When debugging/running the application from Visual Studios, it never stops or crashes, its when I publish it to an IIS Webserver that is has the problem.
I have removed all timers except the clock, and that fixes the issue, so it only seems to happen when there is more than 1 timer.
Single Timer Code
<asp:Timer runat="server" id="clockTimer" interval="1000" ontick="clockTimer_Tick" />
<asp:UpdatePanel runat="server" id="UpdatePanel2" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="clockTimer" eventname="Tick" />
</Triggers>
protected void clockTimer_Tick(object sender, EventArgs e)
{
clockLabel.Text = DateTime.Now.ToString();
}
Example of more than 1 timer code
<asp:Timer runat="server" id="clockTimer" interval="1000" ontick="clockTimer_Tick" />
<asp:UpdatePanel runat="server" id="UpdatePanel2" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="clockTimer" eventname="Tick" />
</Triggers>
protected void clockTimer_Tick(object sender, EventArgs e)
{
clockLabel.Text = DateTime.Now.ToString();
}
<asp:Timer runat="server" id="priTimer" interval="300000" ontick="priTimer_Tick" />
<asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="priTimer" eventname="Tick" />
</Triggers>
<ContentTemplate>
<div class="card border-light mb-3">
<div class="card-header" style="font-size: 1vw; background-color:grey; color:white;" id="priHead">Primary <asp:Label ID="priBadge" runat="server"></asp:Label></div>
<asp:ListBox ID="priList" runat="server" style="font-size: 1vw; OVERFLOW:auto; width:100%"></asp:ListBox>
<div class="card-footer" style="font-size: .25vw; background-color:grey; color:white; max-height:30px;" id="priFoot"><asp:Label ID="priLastfire" runat="server" class="font-italic; text-light;" style="font-size: .5vw;" Text=""></asp:Label></div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
protected void priTimer_Tick(object sender, EventArgs e)
{
priList.Items.Clear();
try
{
string[] priRead = System.IO.File.ReadAllLines(#"D:\Data_Scripts\Primary\priDown.txt");
foreach (string line in priRead)
{
priList.Items.Add(line);
}
priBadge.Text = priList.Items.Count.ToString();
}
catch
{
priList.Items.Add("Error pulling data");
priBadge.Text = "NA";
}
DateTime nextFire = DateTime.Now.AddMilliseconds(priTimer.Interval);
priLastfire.Text = "Last Fire: " + DateTime.Now.ToShortTimeString() + " ----- Next Fire: " + nextFire.ToShortTimeString();
priColor();
}
Screenshot of the 500 error(s)
Thanks in advance - I tried to add as much information and make it as clear as possible.
I have one Ajax UpdateProgress for only one UpdatePanels in the page.
The updatepanel has a gridview with a download button.
Once user clicks on the button, the 'wait' Image shows up, but keeps showing even after the download is complete.
How should I hide it, once the download is done.
My code below but not working, because the image is showing even after the download is complete.
.cs
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton ImageButton1 = (ImageButton)sender;
sIDint = ImageButton1.CommandArgument.ToString();
Thread.Sleep(3000);
HttpCookie cookie = new HttpCookie("ExcelDownloadFlag")
{
Value = "Flag",
Expires = DateTime.Now.AddDays(1)
};
Response.AppendCookie(cookie);
Response.Redirect("pdf.aspx?sID=" + sIDint.ToString());
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "script", "HideImage();", true);
ImageButton imgLike = (ImageButton)FindControl("imgLike");
if (imgLike != null)
{
imgLike.Visible = false;
}
}
.aspx
function HideImage() {
$(#imgLike).hide();
}
<asp:TemplateField HeaderText="Dwn">
<ItemTemplate>
<asp:UpdateProgress ID="UpdateProgress1" runat="server"
AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div class="modal">
<div class="center">
<img alt="" src="/Img/ajax-loader.gif" id="imgLike" />
</div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ImageButton1" />
</Triggers>
<ContentTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="/img/download.gif"
OnClick="ImageButton1_Click"
CommandArgument='<%# Eval("ID") %>' />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
Edit
Solved using
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton ImageButton1 = (ImageButton)sender;
string sIDint = ImageButton1.CommandArgument.ToString();
Thread.Sleep(3000);
HttpCookie cookie = new HttpCookie("ExcelDownloadFlag")
{
Value = "Flag",
Expires = DateTime.Now.AddDays(1)
};
Response.AppendCookie(cookie);
ScriptManager.RegisterStartupScript(this, typeof(string), "OpenWindow", "window.open('pdf.aspx?sID=" + sIDint + "');", true);
}
I've tried to keep this as basic as possible. Basically I just want to see my label update at each interval so I can see my timer is working before I plugin my real code that actually does stuff. So I've tried to go ultra simple and everything I've read tells me with this setup, my label should update with each Timer1_Tick event handler at 1 second intervals. Please tell me what Im missing here.
Here is my ASPX front page:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/>
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="test"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
And my code behind page:
public partial class Default : System.Web.UI.Page
{
public Int32 timeCount=1;
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Timer1_Tick(object sender, EventArgs e)
{
if (timeCount > 0)
{
Label1.Text = (timeCount + 1).ToString();
timeCount++;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
Timer1.Interval = 1000;
}
}
So my expectation is my Labe1.text should start count 2, then 3,4,5 and so on. At 1 second intervals basically. All I ever seem to get is one tick interval to 2 though. That's it though, no more tick events, it's stops at 2. How do I make it so that my button starts the timer and the label just starts updating at the tick intervals until I stop the timer (which in this case is just closing the browser to stop the debug)??
Thanks for you help.
Every timer tick your page is update and your 'timeCount'= 1 then you're checking if(timeCount > 0) <-always true, so the result timeCount + 1 will be 2 again, again and again
Try this: In ASPX front page
<div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Timer runat="server" ID="Timer1" Enabled="False" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label runat="server" ID="Label1"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
And then keep your Counter for example in ViewState
public int TimeCount = 1;
protected int Counter
{
get
{
if (ViewState["count"] == null)
return 0;
int temp;
return int.TryParse(ViewState["count"].ToString(), out temp) ? temp : 0;
}
set { ViewState["count"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["count"] = TimeCount.ToString();
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Counter++;
Label1.Text = Counter.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
}
When ever the condition inside the Timer1_Tick gets satisfied, a postback is trigerred and thus resetting the timeCount varible. So use Session["timeCount"] to store the value instead of the timeCount varible :
protected void Page_Load(object sender, EventArgs e)
{
if (Session["timeCount"] == null)//this avoid resetting of the session on postback
{
Session["timeCount"] = 1;
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Int32 count = Convert.ToInt32(Session["timeCount"].ToString());
if (count > 0)
{
Label1.Text = (count + 1).ToString();
count=count+1;
}
Session["timeCount"]=count;
}
i have same problem as this question.se the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation i havent any problem with <%# Page EnableEventValidation="false" %>.I solve it and i try
protected void Timer1_Tick(object sender, EventArgs e)
{
Label2.Text = Convert.ToString((Convert.ToInt32(Label2.Text) - 1));
if (Convert.ToInt32(Label2.Text) == 0)
{
Timer1.Dispose();
Submit();
}
}
Code work fine means submit () is work if i call from submit button.If its call from Timer_Tick its not work.And timer is not stop or dispose.What is a problem plz suggest?
timer:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text="Remaining Time:(Min)"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="100"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
where are you Enabling or Starting the timer on the page? The timer will not automatically start upon page load, you will need to explicitly call Start() or set Enabled = true
Label2.Text = Convert.ToString((Convert.ToInt32(Label2.Text) - 1));
Does that line works?
Maybe the if
Convert.ToInt32(Label2.Text) == 0
doesn't return true
I have found many solution for my issue but none doesn't work in my scenario.
I have created a test project to demo my concept.
Basically, there is a page that host a user control...
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
WebUserControl1 has a dropdownlist and two other webusercontrols (to be displayed based on the selection of dropdownlist element) inside updatepanel as below.
<%# Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc2" %>
<%# Register Src="WebUserControl3.ascx" TagName="WebUserControl3" TagPrefix="uc3" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<asp:Panel ID="pnlCreditCard" Visible="false" runat="server">
<uc2:WebUserControl2 ID="WebUserControl21" runat="server" />
</asp:Panel>
<asp:Panel ID="pnlGiftCard" Visible="false" runat="server">
<uc3:WebUserControl3 ID="WebUserControl31" runat="server" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Code behind file for WebUserControl1 is .....
public enum PaymentMethod
{
CreditCard = 0,
GiftCard
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
BindPaymentMethods(Enum.GetValues(typeof(PaymentMethod)));
}
private void BindPaymentMethods(Array paymentMethods)
{
DropDownList1.DataSource = paymentMethods;
DropDownList1.DataBind();
if (paymentMethods.Length > 0)
{
DropDownList1.SelectedIndex = 0;
UpdateCreditOrGiftCardPanelVisibility();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateCreditOrGiftCardPanelVisibility();
}
private void UpdateCreditOrGiftCardPanelVisibility()
{
if(DropDownList1.SelectedValue == Enum.GetName(typeof(PaymentMethod),PaymentMethod.CreditCard))
{
pnlGiftCard.Visible = false;
pnlCreditCard.Visible = true;
}
else if (DropDownList1.SelectedValue == Enum.GetName(typeof(PaymentMethod), PaymentMethod.GiftCard))
{
pnlCreditCard.Visible = false;
pnlGiftCard.Visible = true;
}
}
Now, the problem starts here...There is an external javascript file [JScript1.js] (embedded resource) which basically is used to display an alert box.
<script language="javascript" type="text/javascript">
window.onload = function() {
alert('creditcard form');
}
WebUserControl2.ascx.cs code behind is
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptInclude(this.Page, this.Page.GetType().BaseType, "JScript1", Page.ClientScript.GetWebResourceUrl(this.Page.GetType().BaseType, "WebApplication1.JScript1.js"));
}
Alert window doesn't get displayed when I change the dropdownlist value. Even the script is getting registered three times (look in the firebug)
Need to use ScriptInclude instead of ScriptBlock as the original JS file is too big.
Can email the test app....
Thanks in Advance
After working around a bit, I found the solution.
I registered a ScriptManagerProxy in WebUserControl2.ascx
<asp:ScriptManagerProxy ID="ScriptManager1" runat="server" >
<Scripts>
<asp:ScriptReference Assembly="WebApplication1" Name="WebApplication1.JScript1.js" />
</Scripts>
</asp:ScriptManagerProxy>
Then on the code behind of the same control, added...
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ScriptManager.RegisterStartupScript(this, GetType(), "test", "doSomething();", true);
}
and the JScript1.js file looks like below.
function doSomething() {
alert('via dosomething control2 form');
}
Hope that helps. Though I had to litter around more in my practical scenario but this was certainly the way I got it working.
Thanks,