我需要完成的是每 5 秒更新一次标签。为此,我正在使用 UpdatePanel 控件。以下是标记:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="NewsFeed_Item1_Name" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
在服务器端:
protected void Page_Load(object sender, EventArgs e)
{
// Create a timer
myTimer = new System.Timers.Timer();
// Tell the timer what top do when it elapses
myTimer.Elapsed += new ElapsedEventHandler(UpdateNewsFeed);
// Set it to go off every five seconds
myTimer.Interval = 5000;
// And start it
myTimer.Enabled = true;
}
private void UpdateNewsFeed(object sender, ElapsedEventArgs e)
{
//newsfeedCounter and dt are global variables
if (newsfeedCounter >= dt.Rows.Count)
newsfeedCounter = 0;
string name = dt.Rows[newsfeedCounter]["Name"].ToString();
NewsFeed_Item1_Name.Text = name;
newsfeedCounter++;
}
但不知何故,UpdateNewsFeed 功能不会在每 5 秒后触发。
上面的代码有什么问题?