【问题标题】:ASP NET countdown timer with from Label1.Text带有来自 Label1.Text 的 ASP NET 倒数计时器
【发布时间】:2019-03-25 19:12:08
【问题描述】:

是否可以从任何控件(如 Label.Text 或 TextBox.Text)启动带有日期的倒数计时器? 我发现只有一个特定的日期,例如 2018-10-22 03:42:37,但我需要控制该日期。

【问题讨论】:

  • 你的意思是在标签或文本框中显示日期和时间倒计时?
  • 嗨@Leszcz,如果你有足够的权利编辑你的问题和答案,你可以在这里合并你的答案并删除额外的。只需添加一个答案,因此将对其他人有所帮助并直接找到他们的答案

标签: asp.net


【解决方案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;
    }

【讨论】:

    【解决方案2】:

    我有 2 个控件 1 使用 SQL 中的 DataBind(当用户单击插入 DateAdd +12 小时时)日期和秒显示缩减 我正在尝试这个

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

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

    【讨论】:

      【解决方案3】:

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

       <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

      【讨论】:

        猜你喜欢
        • 2016-07-08
        • 2013-09-12
        • 2021-03-29
        • 1970-01-01
        • 1970-01-01
        • 2013-01-26
        • 2019-08-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多