How to prevent asp: Timer from sending a tick to receiving a response?

I have an ASP.NET website. I want the user to make a decision and click one of the given buttons for a certain period of time and redirect it to another page. If he doesnโ€™t make a decision, he will still be redirected with the โ€œno choiceโ€ mark in my head.

I use asp:Timer to initiate a postback after the time has ended. I am doing a redirect by calling Response.Redirect('page.html', false) in the Page_Load handler. In this case, I can handle button_click or timer_tick before actually redirecting. After processing one of these events, the page is executed and redirected.

But I have two problems. Firstly, if the input is processed long enough, the second timer works, a new postback starts, and my current is canceled! Secondly, if the user presses the button just before the time runs out, the timer is marked before the redirection is performed, and it will again override the user's click processing!

How can I solve these problems? How to disable the client-side timer after starting the first tick or after the user clicks a button? Or the best way to solve my problem? Thanks.

+4
source share
2 answers

I got a solution for both problems.

  • I placed asp:Timer inside an asp:UpdatePanel . This is what needs to be done in my case, since I do not need to refresh the whole page until the next question. But I found that in this case the ticks from the timer do not interfere with each other.

  • As described here , I can disable asp:Timer using javascript as follows:

     $find("TimerMaxJudgeTime")._stopTimer(); 

    Then I bind this event to the onclick buttons, and you're done. Since the timer is placed inside the UpdatePanel , it is automatically restored and turned on after the postback.

+1
source

Alternatively, you can use the setTimeout function on the client side instead of controlling the timer and clear the timeout when the button is clicked:

  <script type="text/javascript"> var redirectTimeout = setTimeout(function () { location.href = '<%= ResolveClientUrl("~/page.html") %>'; }, 5000); </script> <asp:Button runat="server" ID="SubmitButton" Text="Submit" OnClientClick="clearTimeout(redirectTimeout);" /> 
+1
source

Source: https://habr.com/ru/post/1435948/


All Articles