我正在使用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方法永远不会运行
答案 0 :(得分:1)
我认为你错过了
timer.Start();