How to set timer in asp.net

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

How can i do this?

+1


source to share


3 answers


If I understand correctly, I think you should be using a client-side (javascript) timer. You cannot use a server timer for this.

When you encounter an error condition, you update the label appropriately and show it to the user. At the same time, you call the client timer, which will defer after 30 seconds.

eg. put the following timer on your page:

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

      

In the event of an error, you must ensure that the client-side timer is started:

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

      



Hope this helps (code not tested since I don't have visual studio right now).

Update:

To "simulate" a click on a button, you need to pass the client ID of the button to the __doPostBack () method, for example:

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

      

For some other possibilities see the following question / answer:

+3


source


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



These are two arguments: EVENTTARGET and EVENTARGUMENT; since you are making this call outside of the normal asp.net loop, you will need to check IsPostBack on the page load event (or init, your choice) - if it is postback, then you will need to look at those two arguments to be collapsed as elements forms (Request.Form ["__ EVENTTARGET"]). Check their value to see if the postback was received from your call or one of the other controls, if the value of those matches is the same as what you are passing from the client side, then make changes to the label test

+1


source


There are two ways to do this, firstly, it is slightly better if you need to call other functions on the same thread. Add ScriptManager and Timer to aspx page, you can remove from toolbar or just enter code. ScriptManager must be declared before asp: Timer. OnTick fires after every interval.

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Timer ID="Timer1" runat="server" Interval="4000" OnTick="Timer1_Tick">
    </asp:Timer>

      

In the code behind (in this case C #):

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("tick tock");
    }

      

The second way is not so great if you need the functions to run on the same thread. You can make a timer in ASP.net using C #, the following code runs the function every 2 seconds. In the code file (.cs):

    // timer variable
    private static System.Timers.Timer aTimer;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a timer and set a two second interval.
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 2000;

        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;

        // Have the timer fire repeated events (true is the default)
        aTimer.AutoReset = true;

        // Start the timer
        aTimer.Enabled = true;
    }

      

Then make the function you want to call in this format:

//Doesn't need to be static if calling other non static functions

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }

      

Output example:

Output

0


source







All Articles