Reputation: 21
Can someone please tell me why this code is not working. I'm guessing it's because there is no postback firing. How do I enable the postback? I am trying to enable a timer that fires a postback and runs a method (simplified here). I need to implement this in .NET 2.0 so can't use UpdatePanel or Ajax Timer.
The output is;
Page Load
Timer Tick
my_method
But the label does not turn visible.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("Page Load");
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 5000;
timer.Enabled = true;
timer.Start();
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
}
protected void timer_Elapsed(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine("Timer Tick");
my_method();
}
protected void my_method()
{
System.Diagnostics.Trace.WriteLine("my_method");
Label1.Visible = true;
}
}
Upvotes: 0
Views: 658
Reputation: 38079
The timer is firing on the server. The client already has the page and will not request an update automatically without something on the client side (like javascript) firing a new request; Once that happens a new version of the page will be rendered.
You will need to implement something in the javascript side to fire an event to look for updated information and then it can update the DOM it has as needed.
Upvotes: 1
Reputation: 273169
The timer is operating on an old discarded instance of the Page. This instance is still in memory but already had its Render phase executed and it will never be used again.
To get a PostBack you need to use the Ajax Timer, or plain JavaScript.
Upvotes: 3