【问题标题】:Asp.NET timer control is not workingAsp.NET 计时器控件不起作用
【发布时间】:2015-03-08 09:17:44
【问题描述】:

我正在使用 Asp.NET 编写一个网站。我添加了一个计时器控件;

<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>

listRecords 的DataSource 是一个名为records 的列表。后面的定时器代码

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();
    }

一切都很好,但连定时器都启用了;

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

它不工作。首先,我认为这是关于代码的。但是我设置了一个断点,发现 timer_tick 方法永远不会运行

【问题讨论】:

  • 这里没有什么明显的错误,但可能.Enabled = true; 没有坚持下去。任何特殊的 ViewState 设置?
  • 你是否也调试过btnSubmit_Click例程,是timer.Enabled = true;行执行?
  • 是的,当然,我还查看了本地人启用的计时器

标签: c# asp.net timer breakpoints


【解决方案1】:

我觉得你不见了

timer.Start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 2011-11-15
    相关资源
    最近更新 更多