计时器勾选不按时间间隔增加值

Jit*_*der 7 c# asp.net

我想增加计时器刻度事件的值,但它没有增加不知道我忘了它只显示1.

<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000"></asp:Timer>

private int i = 0;

protected void Timer1_Tick(object sender, EventArgs e)
{
    i++;

    Label3.Text = i.ToString();
}
Run Code Online (Sandbox Code Playgroud)

VDW*_*WWD 6

您可以使用ViewState存储然后i再次读取值.

int i = 0;

protected void Timer1_Tick(object sender, EventArgs e)
{
    //check if the viewstate with the value exists
    if (ViewState["timerValue"] != null)
    {
        //cast the viewstate back to an int
        i = (int)ViewState["timerValue"];
    }

    i++;

    Label3.Text = i.ToString();

    //store the value in the viewstate
    ViewState["timerValue"] = i;
}
Run Code Online (Sandbox Code Playgroud)