Reputation: 1134
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.
Upvotes: 0
Views: 3613
Reputation: 1
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;
}
Upvotes: 0
Reputation: 599
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;
}
Upvotes: 1