2

I have a label, all what I want is, to display label as "name1" then wait for 5 seconds and then change it to "name2". This is how I did that

protected void Page_Load(object sender, EventArgs e)
{
   Label1.Text = "name1";
   System.Threading.Thread.Sleep(5000);
   Label1.Text = "name2";
}

What it does is it wait's for 5 seconds in total and then display "name2". "name1" is not displaying as I wished. I tried these links,

How do I get my C# program to sleep for 50 msec?

How to trigger a timer tick programmatically?

Did not help. This Using the ASP.NET Timer Control with Multiple UpdatePanel Controls seems to work but this is keep on refreshing the page. I don't want this to happen, it should display name1 then wait for 5 seconds, display name2 and then stop.

Community
  • 1
  • 1
  • If you are using server side code, the page will always refresh. You need to use client-side js to accomplish this. – Filburt May 27 '16 at 12:12

5 Answers5

1

The change you have mentioned must be on the client side, use javascript setTimer method and update the label to whatever text you need.

Pratap Bhaskar
  • 408
  • 3
  • 11
0

You can use Timer control in your page under UpdatePanel for Async functionality (for help on AJAX in ASP.net below are the links provided), and then under its event use this logic for label name change:

 protected void Page_Load(object sender, EventArgs e)
 {
    Timer1.Enabled = true;
    Timer1.Interval = 5000;
 }

 protected void Timer1_Tick(object sender, EventArgs e)
 {
    if(Label1.Text == "name2")
    {
        Label1.Text = "name1";
    }
    else
    {
        Label1.Text = "name2";
    }
 }

for more help on this you can check here and here

sumngh
  • 566
  • 2
  • 10
  • Thank you this is what I am looking for, can you make the timer stop once it's change to name2. –  May 28 '16 at 04:35
  • Yes you can do it just disable the timer as you have enabled it in page load. – sumngh May 29 '16 at 14:54
0

You cannot handle this the way you have in ASP.NET since you will always get the end result from the form_load event. You will have to use JavaScript timing events to handle this at the client side.

      <script>
                $(document).ready(function(){
                 $('#label').val('label1');
                 setTimeout(SetLabel2, 5000); //wait for 5 seconds before setting the label. NOTE: This will keep on repeating until you clear the timeout
                });

                function SetLabel2(){
                  var l = $('#label).val();
                  if(l == 'label2)
                  {
                     window.clearTimeOut(); //So that this does not repeat
                  }
       </script>
Mihir Kale
  • 1,028
  • 1
  • 12
  • 20
0

As suggested by other contributors, you should use client code to obtain that kind of behavior. In your code sample, the Page_Load function simply takes 5 seconds to execute, and when all the processing on the server is done, the resulting HTML is finally sent to the browser... and you see "name2".

Here is a jQuery version of the client code:

$(document).ready(function () {
    $('#Label1').html("name1");
    setTimeout(function () { $('#Label1').html("name2"); }, 5000);
})

and here is a pure Javascript version:

var lbl1 = document.getElementById('Label1');
lbl1.innerHTML = "name1";
setTimeout(function () { lbl1.innerHTML = "name2" }, 5000);

If the ID of the Label is mangled by ASP.NET, you can use <%= Label1.ClientID %> instead of Label1 in the client code.

ConnorsFan
  • 70,558
  • 13
  • 122
  • 146
0

This could be done using only what is provided, it should run once, so once the tick is complete we disable the timer.

protected void Page_Load(object sender, EventArgs e)
{
    Timer1.Enabled = true;
    Label1.Text = "name1";
    Timer1.Interval = 2000;
}

protected void Timer1_Tick(object sender, EventArgs e)
{
    Label1.Text = "name2";
    Timer1.Enabled = false;
}
Saahithyan Vigneswaran
  • 6,841
  • 3
  • 35
  • 45