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



我已经尽可能地保持这一点。基本上,我只想在每个间隔看到我的标签更新,这样我就可以在插入真正的代码之前看到我的计时器在工作。所以我试着变得非常简单,我读到的所有内容都告诉我,对于这个设置,我的标签应该每隔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,则您正在检查(timeCount>0)&lt-始终为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;
        }

最新更新