473,499 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Label is not updated in event callback

Code fragment as simple as possible :

public class TestTimer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Timers.Timer timer1;

private void Page_Load(
object sender,
System.EventArgs e)
{
Label1.Text = "Before timer";
}

private void timer1_Elapsed(
object sender,
System.Timers.ElapsedEventArgs e)
{
Label1.Text = "After timer";
timer1.Stop();
}

private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
this.timer1.Enabled = true;
this.timer1.Interval = 5000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();

}

}

When the timer is elapsed, timer1_elapsed method is called
(I see it in the debugger), line

Label1.Text = "After timer";

runs without any problem, but in fact Label1
remains "Before timer" ! WHY ?

Label1 and timer1 were added automatically, with Web form
designer of Visual Studio .Net, InitializeComponent()
method was generated automatically, and I did not touch it.

Nov 17 '05 #1
2 1912
The problem is that the timer is a server-side control, and while, yes, it
is firing properly, by the time it fires, the page has already been sent to
the client and therefore, updating the label only updates it on the server.
Once the page has been served, nothing that happens on the server will be
sent down to the client.
If you need something on the client side to update after a given interval,
you might want to look into a javascript timer, they seem to have worked
well for me in the past.

HTH,
-Cliff

"Borr" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
Code fragment as simple as possible :

public class TestTimer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Timers.Timer timer1;

private void Page_Load(
object sender,
System.EventArgs e)
{
Label1.Text = "Before timer";
}

private void timer1_Elapsed(
object sender,
System.Timers.ElapsedEventArgs e)
{
Label1.Text = "After timer";
timer1.Stop();
}

private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
this.timer1.Enabled = true;
this.timer1.Interval = 5000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();

}

}

When the timer is elapsed, timer1_elapsed method is called
(I see it in the debugger), line

Label1.Text = "After timer";

runs without any problem, but in fact Label1
remains "Before timer" ! WHY ?

Label1 and timer1 were added automatically, with Web form
designer of Visual Studio .Net, InitializeComponent()
method was generated automatically, and I did not touch it.

Nov 17 '05 #2
because the timer fires after the page has been sent to the client, so the
change to label has no impact. you would need to put in a wait for the timer
in your onload, or at least by prerender, for any changes done by the timer
to appear in the rendered page.

-- bruce (sqlwork.com)

"Borr" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
Code fragment as simple as possible :

public class TestTimer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Timers.Timer timer1;

private void Page_Load(
object sender,
System.EventArgs e)
{
Label1.Text = "Before timer";
}

private void timer1_Elapsed(
object sender,
System.Timers.ElapsedEventArgs e)
{
Label1.Text = "After timer";
timer1.Stop();
}

private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
this.timer1.Enabled = true;
this.timer1.Interval = 5000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elap sed);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();

}

}

When the timer is elapsed, timer1_elapsed method is called
(I see it in the debugger), line

Label1.Text = "After timer";

runs without any problem, but in fact Label1
remains "Before timer" ! WHY ?

Label1 and timer1 were added automatically, with Web form
designer of Visual Studio .Net, InitializeComponent()
method was generated automatically, and I did not touch it.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3906
by: codecraig | last post by:
Hi, When I do something like. s = Scale(master) s.bind("<ENTER>", callback) def callback(self, event): print event.type I see "7" printed out. Where are these constants defined for...
1
3330
by: Henke | last post by:
Hello, I have one ImageButton controls which I initialize in Page_Load and declare on class level. ImageButton save = new ImageButton(); save.ImageUrl = "save.gif" save.Click += new...
1
1218
by: ProgramAnalyst | last post by:
Hey all, I have a label that gets its text from a DB. This text can be changed on the page in a textbox and after the save button is clicked the new text is updated in the DB and the page...
2
1612
by: ibiza | last post by:
Hi all, I have a label on a webpage that shows the result of a calculation. When the page is loaded, the label text is "", and is updated with javascript via its innerText property. Once I click...
3
5410
by: ginguene | last post by:
Its supposed to be a very simple program, with a class to display the updated current time every second (CAnalogTime) inside a Label in my form, using ElapsedEventHandler from System.Timers; The...
2
1193
by: pallaw | last post by:
Hello All, My web Client is using a COM component with connection point enabled.my client is getting call back from this component.now I have to update a text control in handler of this...
2
2565
by: =?Utf-8?B?Y2FzaGRlc2ttYWM=?= | last post by:
Hi, I have a Label on a Windows form (Version 1.1.4322) and while I iterate recursively through a method I want to show the name of the current file being copied to another directory. The...
1
3000
by: hainguyen2x | last post by:
I'm trying to get a notification from the NT event for any new event using the DispatchWithEvents() function. Everything seems to be working the way I wanted, but I don't know how to get the...
18
3914
by: navyjax2 | last post by:
What if your event handler has to be nonstatic?
0
7225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.