tags:

views:

1156

answers:

2

I have a page where when some operation goes wrong i want to start the timer, wait 30 second, stop the timer and retry the operation. Every time the timer starts I need to inform the user about it by changing some label's text.

How can I do it?

+1  A: 

from the client side to force a postback you can directly call the __doPostBack method

It takes two arguments, EVENTTARGET and EVENTARGUMENT; since you are making this call outside the normal asp.net cycle you will need to check IsPostBack on your page load event (or init, your choice) - if it is a postback then you will need to look at those two argument which get rolled up as form elements (Request.Form["__EVENTTARGET"]). Check their value to see if the postback came from your call or one of the other controls, if the value of those matches what you pass in from the client side then make your change to the label test

keithwarren7
+3  A: 

If I understand correctly, the I think you should use a client-side (javascript) timer instead. You can not use a server-side timer for this.

When you detect the error-condition, you update the label accordingly and display it to the user. At the same time you invoke a client-side timer which will postback after 30 seconds.

E.g. put the following timer-code onto your page:

  <script>
    function StartTimer()
    {
      setTimeout('DoPostBack()', 30000); // call DoPostBack in 30 seconds
    }
    function DoPostBack()
    {
      __doPostBack(); // invoke the postback
    }
  </script>

In case of the error-condition, you have to make sure that the client-side timer will be started:

if (error.Code == tooManyClientsErrorCode)
{
  // add some javascript that will call StartTimer() on the client
  ClientScript.RegisterClientScriptBlock(this.GetType(), "timer", "StartTimer();", true);
  //...
}

I hope this helps (the code is not tested, since I don't have visual studio available right now).

Update:

To "simulate" a button click, you have to pass to button's client id to the __doPostBack() method, e.g:

function DoPostBack()
{
  var buttonClientId = '<%= myButton.ClientID %>';
  __doPostBack(buttonClientId, ''); // simulate a button click
}

For some other possibilities, see the following question/answer:

M4N
this is not what i'm trying to do. when i detect an error situation i show the message to the user (this is the moment when i thought i should do postback - to refresh site so that new text will be displayed) and after 30 seconds retry the failed operation - and that works fine.
agnieszka
not true - i checked it again and it doesn't work.
agnieszka
ok what you wrote could be useful for me but instead of calling postback in 30 seconds time, is it possible to somehow click a button on a page from the javascript code, like it was clicked be the user?
agnieszka
see my updated answer
M4N