Search code examples
ajaxvb.nettimer

Execute ASP.nett codebehind method with Ajax Timer


I'm trying to get a timer to invoke a codebehind method after 5 seconds, but when the page is being loaded I get

Method 'Protected WithEvents timEndup As System.Web.UI.Timer' does not have a signature compatible with delegate 'Delegate Sub EventHandler(Of System.EventArgs)(sender As Object, e As System.EventArgs)'.

On the page I have

<asp:ScriptManager runat="server" />
<asp:Timer runat="server" id="timEndup" interval="5000" ontick="timEndup" />

and in the codebehind I have

Private Sub timEndup_Tick(sender As Object, e As System.EventArgs) Handles timEndup.Tick
    Stop
End Sub

The signature looks correct to me, so what am I doing wrong?


Solution

  • You have already associated to an EventHandler your timEndup’s .Tick Event by: Handles timEndup.Tick in code behind.

    So, just remove OnTick attribute by client side, OnTick="timEndup" must be removed

    Or remove: Handles timEndup.Tick by code behind and put in the OnTick attribute the real name of EventHandler Sub as timEndup_Tick

    So the code becomes:

    Client Side:

    <asp:ScriptManager runat="server" />
    <asp:Timer runat="server" ID="timEndup" Interval="5000"  OnTick="timEndup_Tick"  />
    

    Server Side:

    Protected Sub timEndup_Tick(sender As Object, e As EventArgs)
       Debug.WriteLine("Tick ")
    End Sub
    

    Or:

    Client Side:

    <asp:ScriptManager runat="server" />
    <asp:Timer runat="server" ID="timEndup" Interval="5000" />
    

    Server Side:

     Protected Sub timEndup_Tick(sender As Object, e As EventArgs) Handles timEndup.Tick
          Debug.WriteLine("Tick ")
     End Sub
    

    Another thing is that the EventHandler Sub must be declared NOT Private