locked
Using Timer and Update Panel to update a label RRS feed

  • Question

  • User1540670854 posted

    Hi

    I appreciate your help with this. I am creating a very simple <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="9" data-gr-id="9">aspx</g> page with 1 label and 1 button. I am trying to update the label every 200 ms by adding 1 to the previous value. when I run the program the label stops at 1:

    Public Class WebForm1
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
        Dim c = 0
        Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            c = c + 1
            Label1.Text = c
        End Sub
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Interval = 200
    
            Timer1.Enabled = True
    
        End Sub
    End Class
    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication13.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
    <ContentTemplate> <div> </div> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:Button ID="Button1" runat="server" Text="Button" /> </ContentTemplate> </asp:UpdatePanel> <asp:Timer ID="Timer1" runat="server"> </asp:Timer> </form> </body> </html>

    Can you please tell me what I am doing wrong?

    Cheers

    </div> </div>

    Saturday, June 16, 2018 3:20 PM

Answers

  • User283571144 posted

    Hi raefkobeissi,

    As far as I know, if the timer trigger the tick event, the server will run all the code inside the TimerWithUpdatePanel page code-behind.

    So it will reset the c to 0 and plus 1 again. You will find it always show 1.

    I suggest you could set a breakpoint at the page load event to see how the page works. This is related with the asp.net web form page life.

    I suggest you could consider directly get the current textbox value and plus 1.

    It will work well.

    More details, you could refer to below codes:

    ASPX:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TimerWithUpdatePanel.aspx.vb" Inherits="VBWebform.TimerWithUpdatePanel" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                    </Triggers>
                    <ContentTemplate>
                        <div>
                        </div>
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                        <asp:Button ID="Button1" runat="server" Text="Button" />
                    </ContentTemplate>
                </asp:UpdatePanel>
                <asp:Timer ID="Timer1" runat="server">
                </asp:Timer>
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
        Dim c = 0
        Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            If Label1.Text <> "Label" Then
                c = Convert.ToInt32(Label1.Text)
            End If
            c = c + 1
            Label1.Text = c
        End Sub
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Interval = 1000
            Timer1.Enabled = True
        End Sub

    Result:

    Rest Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 19, 2018 9:14 AM

All replies

  • User475983607 posted
    IMHO, It’s unrealistic to make 5 requests a second. However, the following SO post shows the syntax.

    https://stackoverflow.com/questions/3523470/timer-in-updatepanel
    Saturday, June 16, 2018 4:00 PM
  • User1540670854 posted

    Thank you for your reply. I agree with you. I changed the request to 1 request every 5 seconds. It is still not working. I also checked the link and to be honest, it wasn't different compared to my code. When I press the button, the Label counts 1 and then stops, it doesn't update from there onward.

    Your help is much appreciated.

    Saturday, June 16, 2018 4:07 PM
  • User475983607 posted

    Thank you for your reply. I agree with you. I changed the request to 1 request every 5 seconds. It is still not working. I also checked the link and to be honest, it wasn't different compared to my code. When I press the button, the Label counts 1 and then stops, it doesn't update from there onward.

    Your help is much appreciated.

    Perhaps this tutorial is more direct.

    https://msdn.microsoft.com/en-us/library/bb386404.aspx

    Saturday, June 16, 2018 5:59 PM
  • User283571144 posted

    Hi raefkobeissi,

    As far as I know, if the timer trigger the tick event, the server will run all the code inside the TimerWithUpdatePanel page code-behind.

    So it will reset the c to 0 and plus 1 again. You will find it always show 1.

    I suggest you could set a breakpoint at the page load event to see how the page works. This is related with the asp.net web form page life.

    I suggest you could consider directly get the current textbox value and plus 1.

    It will work well.

    More details, you could refer to below codes:

    ASPX:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TimerWithUpdatePanel.aspx.vb" Inherits="VBWebform.TimerWithUpdatePanel" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                    </Triggers>
                    <ContentTemplate>
                        <div>
                        </div>
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                        <asp:Button ID="Button1" runat="server" Text="Button" />
                    </ContentTemplate>
                </asp:UpdatePanel>
                <asp:Timer ID="Timer1" runat="server">
                </asp:Timer>
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
        Dim c = 0
        Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            If Label1.Text <> "Label" Then
                c = Convert.ToInt32(Label1.Text)
            End If
            c = c + 1
            Label1.Text = c
        End Sub
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Interval = 1000
            Timer1.Enabled = True
        End Sub

    Result:

    Rest Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 19, 2018 9:14 AM
  • User1540670854 posted
    Thank you for your reply! I managed to make it work on my localhost but once uploaded to the server which allows AJAX it just doesnt work at all when I click the button. It looks like the timer doesn’t appear to be working. I am wondering if I am missing something in the web.config file . On Localhost it works perfectly!
    Something simple I am missing there that is making it not work on the server.
    Tuesday, June 19, 2018 10:48 AM
  • User283571144 posted

    Hi raefkobeissi,

    Thank you for your reply! I managed to make it work on my localhost but once uploaded to the server which allows AJAX it just doesnt work at all when I click the button. It looks like the timer doesn’t appear to be working. I am wondering if I am missing something in the web.config file . On Localhost it works perfectly!

    According to your description, it is too hard directly find out the reason why it doesn't work well on the server,

    Do you have any error message?

    I suggest you could use f12 developtool's network tag to see how the ajax works and check the error message.

    Besides, since this issue is not related with your first thread, I suggest you could firstly mark the helpful reply as answer and start a new thread to talk about your new issue.

    This will help other people who faces the same issue.

    Best Regards,

    Brando

    Tuesday, June 19, 2018 12:49 PM
  • User1540670854 posted

    Thank you, I created a new thread as advised:

    https://forums.asp.net/p/2142524/6213057.aspx?p=True&t=636649855446131352

    Tuesday, June 19, 2018 1:14 PM