I have an ASP.NET web site. I want a user to make a decision and click one of the given buttons within a certain amount of time and redirect him to another page. If he didn't make a decision, he still will be redirected with "no choice made" mark in my head.
I use asp:Timer
to initiate postback after time is up. I'm doing redirect by calling Response.Redirect('page.html', false)
in Page_Load
handler. In this case I can handle button_click
or timer_tick
events before actually redirecting. After one of those events is processed, page is rendered and redirect is done.
But I have two problems. First, if input is processed long enough, second timer's tick is triggered, new postback initiated, and my current is canceled! Second, if user click a button just before the time is up, the timer tick occurs before redirect is done and it again overrides handling of user click!
How can I resolve these issues? How can I disable timer on client side after the first tick is triggered or after a user have clicked a button? Or what is the better way of solving my task? Thanks.
As a variant you may use client-side setTimeout
function instead of Timer control and clear timeout on button click:
<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);" />
I got the solution for both problems.
I placed asp:Timer
inside an asp:UpdatePanel
control. This is the right thing to do in my case since I don't need to refresh entire page before the next question. But I discovered that in this case ticks from timer don't interfere with each other.
As described here, I can disable asp:Timer
using javascript like this:
$find("TimerMaxJudgeTime")._stopTimer();
Then I bind this to my buttons' onclick
event and I'm done. Since timer is placed inside an UpdatePanel
, it is autmatically recreated and enabled after a postback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With