cameloper
cameloper

Reputation: 306

Asp.NET timer control is not working

I am programming a website using Asp.NET. I added a timer control;

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:Timer ID="timer" runat="server" Interval="1000" OnTick="timer_Tick" Enabled="False"></asp:Timer>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:ListBox ID="listRecords" runat="server" Style="position: absolute; top: 13px; left: 289px; height: 248px; width: 250px; background: rgba(255, 255, 255, 0.72)" Font-Names="Arial" Font-Size="X-Large" AutoPostBack="True"></asp:ListBox>
            </ContentTemplate>
            <Triggers>

                <asp:AsyncPostBackTrigger ControlID="timer" EventName="Tick" />

            </Triggers>
        </asp:UpdatePanel>

DataSource of listRecords is a list named records. Timer's code behind

protected void timer_Tick(object sender, EventArgs e)
    {
        for(int a=0;a<Records.Count;a++)
        {
            var item = Records[a];
            int seconds = int.Parse(item.Substring(item.Length - 2));
            int minutes = int.Parse(item.Substring(item.Length - 4, 1));
            string time = null;
            if (seconds != 0)
            {
                seconds--;
                time = minutes.ToString() + ":" + seconds.ToString();
            }
            if (seconds == 0)
            {
                seconds = 59;
                minutes--;
                time = minutes.ToString() + ":" + seconds.ToString();
            }
            string itemPlate = item.Substring(0, item.Length - 5);
            string itemString = itemPlate + time;
            Records[a] = itemString;
        }
        listRecords.DataBind();
    }

Everything is fine, but even the timer is enabled;

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ...
        timer.Enabled = true;
    }

it's not working. First I thought it was about the code. But I set a breakpoint and saw timer_tick method never runs

Upvotes: 2

Views: 2688

Answers (1)

Anoop M V
Anoop M V

Reputation: 19

I think you are missing

timer.Start();

Upvotes: 0

Related Questions