Label is not updated in event callback

Discussion in 'ASP .Net' started by Borr, Nov 5, 2003.

  1. Borr

    Borr Guest

    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_Elapsed);
    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.
     
    Borr, Nov 5, 2003
    #1
    1. Advertising

  2. Borr

    Cliff Harris Guest

    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" <> wrote in message
    news:0a9801c3a3c2$6b9a40c0$...
    > 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_Elapsed);
    > 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.
    >
     
    Cliff Harris, Nov 5, 2003
    #2
    1. Advertising

  3. Borr

    bruce barker Guest

    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" <> wrote in message
    news:0a9801c3a3c2$6b9a40c0$...
    > 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_Elapsed);
    > 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.
    >
     
    bruce barker, Nov 5, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper
    Replies:
    2
    Views:
    768
    Scott Mitchell [MVP]
    May 7, 2005
  2. ibiza
    Replies:
    2
    Views:
    3,998
    ibiza
    Feb 28, 2006
  3. Dave Weeden

    Controls not updated by callback event

    Dave Weeden, Aug 20, 2007, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    925
    Walter Wang [MSFT]
    Aug 22, 2007
  4. Dmitry Korolyov

    Datagrid not updated during delete, but updated during insert and update

    Dmitry Korolyov, Sep 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    439
    Dmitry Korolyov
    Sep 22, 2003
  5. Knut Krueger
    Replies:
    8
    Views:
    324
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page