简体   繁体   English

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

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

I've tried to keep this as basic as possible. 我试图使这尽可能基本。 Basically I just want to see my label update at each interval so I can see my timer is working before I plugin my real code that actually does stuff. 基本上,我只想查看每个时间间隔的标签更新,以便可以在插入实际起作用的真实代码之前看到计时器正在工作。 So I've tried to go ultra simple and everything I've read tells me with this setup, my label should update with each Timer1_Tick event handler at 1 second intervals. 因此,我尝试变得非常简单,并且我阅读的所有内容都告诉我此设置,我的标签应该每隔1秒间隔随每个Timer1_Tick事件处理程序更新。 Please tell me what Im missing here. 请告诉我我在这里缺少什么。

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

And my code behind page: 我的代码在页面后面:

    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;
    }
}

So my expectation is my Labe1.text should start count 2, then 3,4,5 and so on. 所以我的期望是我的Labe1.text应该开始计数2,然后是3、4、5,依此类推。 At 1 second intervals basically. 基本上以1秒的间隔。 All I ever seem to get is one tick interval to 2 though. 我似乎只能得到一个滴答声间隔到2。 That's it though, no more tick events, it's stops at 2. How do I make it so that my button starts the timer and the label just starts updating at the tick intervals until I stop the timer (which in this case is just closing the browser to stop the debug)?? 就是这样,不再有滴答声事件,它停止在2。我如何做到这一点,以便我的按钮启动计时器,并且标签仅在滴答间隔开始更新,直到我停止计时器(在这种情况下只是关闭计时器)。浏览器停止调试)??

Thanks for you help. 感谢您的帮助。

Every timer tick your page is update and your 'timeCount'= 1 then you're checking if(timeCount > 0) <-always true, so the result timeCount + 1 will be 2 again, again and again 每个计时器在您的页面上打动更新并且您的'timeCount'= 1时,您正在检查if(timeCount> 0)<-始终为true,因此结果timeCount + 1将一次又一次地变为2

Try this: In ASPX front page 尝试以下操作:在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>

And then keep your Counter for example in ViewState 然后将您的Counter保留在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;
    }

When ever the condition inside the Timer1_Tick gets satisfied, a postback is trigerred and thus resetting the timeCount varible. 只要满足Timer1_Tick内部的条件,就会触发回发,从而重置timeCount变量。 So use Session["timeCount"] to store the value instead of the timeCount varible : 因此,使用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;
        }

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

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