来自Label1.Text的ASP NET倒数计时器

时间:2018-10-21 15:39:50

标签: asp.net

是否可以从Label.Text或TextBox.Text之类的控件中使用日期启动倒数计时器? 我发现只有特定日期,例如2018-10-22 03:42:37,但我需要从控件中获取该日期。

3 个答案:

答案 0 :(得分:1)

您可以简单地添加一个计时器控件,并将计时器的值显示在任何标签上

 <asp:timer id="Timer1" runat="server" interval="1000" ontick="Timer1_Tick" xmlns:asp="#unknown"></asp:timer>
<asp:lable id="Lable1" runat="server" xmlns:asp="#unknown" />

仅请参见此链接中的示例 Countdown timer on ASP.NET page

https://www.youtube.com/watch?v=5yyXtIvyYxc

或者,如果只显示

,则可以使用javascript
<script type="text/javascript">

    function countdown() 
    {
        seconds = document.getElementById("timerLabel").innerHTML;
        if (seconds > 0) 
        {
            document.getElementById("timerLabel").innerHTML = seconds - 1;
            setTimeout("countdown()", 1000);
        }
    }

    setTimeout("countdown()", 1000);

</script>

Countdown timer on ASP.NET page

答案 1 :(得分:1)

我有2个控件1个来自SQL的DataBind(当用户单击时,插入DateAdd +12小时)日期和第二个秀场显示的减少时间 我尝试过这个

https://www.w3schools.com/howto/howto_js_countdown.asp

但是这种方式不会从控件中读取日期。

答案 2 :(得分:1)

我找到了解决方法 这是我的我不记得谁做菜,但要为他赞扬。

       protected void Page_Load(object sender, EventArgs e)
    {


        if (!Page.IsPostBack)
        {
            SqlConnection con = new SqlConnection("server=localhost; database=DB_TS; trusted_connection=true;");
            con.Open();

            string queryString = @"Select * from Vote";
            SqlCommand cmd = new SqlCommand(queryString, con);

            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                TextBox1.Text = dr["Data2"].ToString();

            }

            dr.Close();
            cmd.ExecuteNonQuery();

            con.Close();                

        }


        DataTable dt = new DataTable();
        DateTime startDate = DateTime.Now;
        DateTime dt2 = DateTime.Parse(TextBox1.Text);
        DateTime endDate = Convert.ToDateTime(dt2.ToString());
        Label1.Text = CalculateTimeDifference(startDate, endDate);


    }

    public string CalculateTimeDifference(DateTime startDate, DateTime endDate)
    {
        int days = 0; int hours = 0; int mins = 0; int secs = 0;
        string final = string.Empty;
        if (endDate > startDate)
        {
            days = (endDate - startDate).Days;
            hours = (endDate - startDate).Hours;
            mins = (endDate - startDate).Minutes;
            secs = (endDate - startDate).Seconds;
            final = string.Format("{0} days {1} hours {2} mins {3} secs", days, hours, mins, secs);
        }
        return final;
    }