Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to start the count down timer after I click on the submit button instead of when the page is loaded
protected void Timer1_Tick(object sender, EventArgs e)
        {
            if (TimeAllSecondes > 0)
            {
                TimeAllSecondes = TimeAllSecondes - 1;
            }

            TimeSpan time_Span = TimeSpan.FromSeconds(TimeAllSecondes);
            hh = time_Span.Hours;
            mm = time_Span.Minutes;
            ss = time_Span.Seconds;

            Label2.Text = "  " + hh + ":" + mm + ":" + ss;
        }

protected void Button1_Click(object sender, EventArgs e)
       {

      }


What I have tried:

I have tried calling the Timer1_Tick method as Timer1_Tick(sender, e) within the Button1_Click method , but it didn't work.
Posted
Updated 8-Aug-17 19:10pm
Comments
F-ES Sitecore 9-Aug-17 4:31am    
That code is never going to work, your browser needs to request html from the server via a request which it then shows. You can't "push" content to the browser from your server code, it has to be requested, so a timer updating "Label2" server-side is not going to have Label2 updated on the client.

You'll need to solve this using client-side scripting and maybe some ajax depending on what your aim is. If it's just to show a countdown or timer then google "javascript timer" and you'll find lots of examples.

1 solution

try this

protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack) {
               Timer1.Enabled = false;
           }
       }

       protected void Button1_Click(object sender, EventArgs e)
       {
           Timer1.Enabled = true;
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900