ASP.NET AJAX---Timer&HiddenField control small example (to achieve countdown)

①Default.aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        罗罗诺亚.索隆<br />             
            <asp:Button ID="Button1" runat="server" Text="剩余时间:10" Enabled="False" />
            <br />
            <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">
            </asp:Timer>
            <asp:HiddenField ID="HiddenField1" runat="server" />
            <br />
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

②Default.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)  //IsPostBack页面首次加载该属性的值为false
        {
            HiddenField1.Value = "10";
            Button1.PostBackUrl = "~/Default2.aspx";
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (HiddenField1.Value == "0")
        {
            Button1.Enabled = true;
            Timer1.Enabled = false;
            Button1.Text = "现在时间到了";
            HiddenField1.Value = "yes";

        }
        else
        {
            HiddenField1.Value = (int.Parse(HiddenField1.Value) - 1).ToString();  //每隔一秒,让HiddenField1的value减1,直至减为零
            Button1.Text = "剩余时间:" + HiddenField1.Value + "秒";

        }
    }
}

③Default2.aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    受尽苦难而不厌,此乃修罗之道。<br />  //点击之后的跳转页面(文字)
    </div>
    </form>
</body>
</html>

④Default2.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HiddenField hd = null;
        try
        {
            hd = (HiddenField)PreviousPage.FindControl("HiddenField1");
        }
        catch
        {
            Response.Redirect("~/Default.aspx");
        }
        if (hd.Value == "yes")
        {
            Response.Write("罗罗诺亚.索隆");
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}

⑤The effect diagram is as follows:

Insert picture description here
Insert picture description here
Insert picture description here
⑥Description:
Ⅰ. Display countdown.
Ⅱ. The countdown is over 10 seconds.
Ⅲ. Click "The time is up" to jump to the Default2.aspx page.

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/115337242
Recommended