简体   繁体   English

ASP.NET更新标签分别和异步

[英]ASP.NET update labels separately and asynchronously

Ok so I can successfully update my page labels asynchronously using a timer. 好的,这样我就可以使用计时器成功地异步更新页面标签。 The problem is that the labels always update simultaneously while I want each one to update in sequence. 问题是标签总是同时更新,而我希望每个标签都按顺序更新。

Here is my aspx: 这是我的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>

And my codebehind: 而我的代码背后:

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

    }

}
}

After trying several examples I decided to ask here and see if I was missing something obvious. 在尝试了几个示例之后,我决定在这里询问,看看我是否缺少明显的东西。 Somehow my counter variable is being reset to 0 which tells me that the entire page may be being rerun each tick instead of just the Timer1_Tick method. 不知何故,我的计数器变量被重置为0,这告诉我整个页面可能正在重新运行每个刻度,而不仅仅是Timer1_Tick方法。

The clause where counter == 1 also increments counter , so by the end of the clause, counter = 2, hence the other condition is also true. 其中counter == 1的子句也会增加counter ,因此在该子句的末尾counter = 2,因此其他条件也成立。

Don't increment counter within the counter == 1 clause. 不要在counter == 1子句中增加counter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM