[英]ASP.NET update labels separately and asynchronously
好的,這樣我就可以使用計時器成功地異步更新頁面標簽。 問題是標簽總是同時更新,而我希望每個標簽都按順序更新。
這是我的aspx:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel runat="server" id="UpdatePanel1"
UpdateMode="Conditional">
<contenttemplate>
<asp:Timer id="Timer1" runat="server"
Interval="5000"
OnTick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label1" runat="server" >Label1</asp:Label>
<asp:Label ID="Label2" runat="server" >Label2</asp:Label>
</contenttemplate>
</asp:UpdatePanel>
</asp:Content>
而我的代碼背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
public partial class _Default : Page
{
public int counter = 0;
protected void Timer1_Tick(object sender, EventArgs e)
{
counter++;
System.Diagnostics.Debug.WriteLine("Counter: " + counter);
if (counter == 1)
{
Label1.Text = "<font color='green'>Started</font>";
UpdatePanel1.Update();
}
if (counter == 2)
{
Label2.Text = "<font color='green'>Started</font>";
UpdatePanel1.Update();
}
}
}
在嘗試了幾個示例之后,我決定在這里詢問,看看我是否缺少明顯的東西。 不知何故,我的計數器變量被重置為0,這告訴我整個頁面可能正在重新運行每個刻度,而不僅僅是Timer1_Tick方法。
其中counter == 1
的子句也會增加counter
,因此在該子句的末尾counter
= 2,因此其他條件也成立。
不要在counter == 1
子句中增加counter
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.