Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Timer and Update Panel

Hi I have searched a lot on stack overflow to find suitable answer to my question but could not find it. I hope you guys will help me with my problem.

I have a web application which have two update panels.

1st update panel only contains a timer whose interval is only one second and it acts as count down timer.

2nd update panel is conditional panel which only updates when countdown is finished.

The problem I am facing is that when I publish my web application the timer does not seems to work right the page some how used to come at top (I assume it get refresh). On local host it works perfectly without any trouble. The code is given below for aspx file and aspx.cs file

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
  <ContentTemplate>
    <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
    </asp:Timer> 
    <div class="form-area">
      <div class="clock">
        <p><img id="c-time" src="images/clock.png"/>Current Time 
           <span class="spanclass">
             <asp:Label ID="CurrentTimeLabel" runat="server" Text="Label">
             </asp:Label>
           </span>
        </p>
      </div>
      <p id="text">
        <asp:Label ID="Processing" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Label ID="FilterLabel" runat="server" Text="Label"></asp:Label>
        <br />Time Left for list to Refresh
        <span class="spanclass">
          <asp:Label ID="TimeLeftLabel" runat="server" Text=""></asp:Label>
        </span>
      </p>
    </div>
    <div class="clear"></div>
  </ContentTemplate>
</asp:UpdatePanel>

and for aspx.cs

protected void Timer1_Tick(object sender, EventArgs e)
    {
        CurrentTimeLabel.Text = DateTime.Now.ToShortTimeString();
        if (SecondsLeft > 0)
        {
            SecondsLeft--;
        }

        else if (MinutesLeft > 0)
        {
            MinutesLeft--;
            SecondsLeft = 59;
        }
        if (SecondsLeft < 10)
        TimeLeftLabel.Text =  " 0" + MinutesLeft + ":" + "0" + SecondsLeft;
        else
        TimeLeftLabel.Text = " 0" + MinutesLeft + ":" + SecondsLeft;
        if (MinutesLeft == 5)
        {
            FilterLabel.Text = "";
        }
        if (MinutesLeft == 0 && SecondsLeft == 0)
        {
           function();
        }

    }

Thanks in advance. Looking forward for help

like image 633
jibran musa Avatar asked Nov 08 '13 11:11

jibran musa


1 Answers

I agree with Ovidiu. Try it in javascript with jQuery.

$(document).ready(function(){
  setInterval(function () {Counter() }, 1000);
 });

var count= 0;
function Counter() {
  count++;
  $('#testLabel').html(count);
}
like image 60
Jeroen Doppenberg Avatar answered Oct 04 '22 02:10

Jeroen Doppenberg