C# ASP.NET 应用程序中的计时器和按钮似乎只给出一个滴答事件

Timer and button in C# ASP.NET app only giving one tick event it seems

我尽量保持基本的状态。基本上我只想在每个时间间隔看到我的标签更新,这样我就可以在我插入实际执行操作的真实代码之前看到我的计时器正在工作。所以我试图变得非常简单,我读过的所有内容都告诉我这个设置,我的标签应该以 1 秒的间隔更新每个 Timer1_Tick 事件处理程序。请告诉我我在这里缺少什么。

这是我的 ASPX 首页:

    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
    <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000" />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
          <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/>
        </Triggers>
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server" Text="test"></asp:Label>
        </ContentTemplate>
       </asp:UpdatePanel>
    </div>
</form>

还有我的页面隐藏代码:

    public partial class Default : System.Web.UI.Page
  {
    public Int32 timeCount=1;
    protected void Page_Load(object sender, EventArgs e)
    { }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (timeCount > 0)
        {
            Label1.Text = (timeCount + 1).ToString();
            timeCount++;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Timer1.Enabled = true;
        Timer1.Interval = 1000;
    }
}

所以我的期望是我的 Labe1.text 应该开始计数 2,然后是 3、4、5 等等。基本上以 1 秒为间隔。不过,我似乎得到的只是一个滴答间隔到 2。就是这样,没有更多的滴答事件,它在 2 处停止。我如何做到这一点,以便我的按钮启动计时器并且标签只是在滴答间隔开始更新,直到我停止计时器(在这种情况下只是关闭浏览器停止调试)??

感谢您的帮助。

每次计时器滴答更新您的页面并且您的 'timeCount'= 1 然后您正在检查 if(timeCount > 0) <-always true,因此结果 timeCount + 1 将再次为 2,再次和再次

试试这个:在 ASPX 首页

<div>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    <asp:Timer runat="server" ID="Timer1" Enabled="False" Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
    <asp:UpdatePanel runat="server" ID="UpdatePanel1">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:Label runat="server" ID="Label1"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

然后将您的计数器保存在 ViewState 中,例如

public int TimeCount = 1;

    protected int Counter
    {
        get
        {
            if (ViewState["count"] == null)
                return 0;
            int temp;
            return int.TryParse(ViewState["count"].ToString(), out temp) ? temp : 0;
        }
        set { ViewState["count"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["count"] = TimeCount.ToString();
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Counter++;
        Label1.Text = Counter.ToString();
    }

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

只要满足 Timer1_Tick 中的条件,就会触发回发,从而重置 timeCount 变量。所以使用 Session["timeCount"] 来存储值而不是 timeCount 变量:

      protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["timeCount"] == null)//this avoid resetting of the session on postback
            {
                Session["timeCount"] = 1;
            }

        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Int32 count = Convert.ToInt32(Session["timeCount"].ToString());
            if (count > 0)
            {
                Label1.Text = (count + 1).ToString();
                count=count+1;
            }
            Session["timeCount"]=count;
        }