简体   繁体   English

ASP.NET C#AJAX控件倒数计时器仅在对未来时间进行硬编码时才有效

[英]ASP.NET C# AJAX Control Countdown Timer Works Only When Future Time is Hardcoded

I have a AJAX C# ASP.NET control and it works properly (counts down from 2 hours from current time only when the datetime is hardcoded. I would like to pass in dates as a variable but every time I do so, the timer stops working. 我有一个AJAX C#ASP.NET控件,它可以正常工作(只有在对日期时间进行硬编码时,才从当前时间开始倒数2小时。我想将日期作为变量传递,但是每次这样做,计时器都会停止工作。

Here's what works... 这是有效的...

 protected void Timer1_Tick(object sender, EventArgs e)
    {
        String DateTimeFuture = DateTime.Now.AddHours(2).ToString();
        DateTime NeededByDateTime = DateTime.Parse("03/18/2017 10:00:00 AM");
        TimeSpan time1 = new TimeSpan();
        time1 = NeededByDateTime - DateTime.Now;
        if ((time1.Hours == 0) && (time1.Minutes == 0) && (time1.Seconds == 0))
        {
            Label1.Text = "Time Expired!";
            //Return article to results
        }
        else
        {
            string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til launch.", time1.Days, time1.Hours, time1.Minutes, time1.Seconds);
            Label1.Text = countDown.ToString();
        }

    }

Here's what doesn't work... 这是行不通的...

protected void Timer1_Tick(object sender, EventArgs e)
    {
        String DateTimeFuture = DateTime.Now.AddHours(2).ToString();
        DateTime NeededByDateTime = DateTime.Parse(DateTimeFuture);
        TimeSpan time1 = new TimeSpan();
        time1 = NeededByDateTime - DateTime.Now;
        if ((time1.Hours == 0) && (time1.Minutes == 0) && (time1.Seconds == 0))
        {
            Label1.Text = "Time Expired!";
        }
        else
        {
            string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til launch.", time1.Days, time1.Hours, time1.Minutes, time1.Seconds);
            Label1.Text = countDown.ToString();
        }

    }

Any ideas as to why it isn't working for the second piece of code? 关于为什么它不适用于第二段代码的任何想法?

UPDATE: 更新:

If you would like to try to get the example working for your environment, you'll need the following code in your page. 如果您想尝试使示例适用于您的环境,则在页面中将需要以下代码。

 <form method="post" runat="server" action="Page.aspx">
                <asp:ScriptManager ID="ScriptManager1" runat="server" />

            <asp:UpdatePanel ID="UpdatePanel1" Visible="true" runat="server" >
                <ContentTemplate>
                    <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
                    </asp:Timer>
                    <asp:Label ID="Label1" runat="server"></asp:Label>
                </ContentTemplate>
            </asp:UpdatePanel>
</form>

What exactly doesn't work? 到底什么不起作用? Does it throw an exception. 它会引发异常吗? What is the value of the contentNeededByDateTime variable after you parse the date? 解析日期后,contentNeededByDateTime变量的值是什么? Is it correct? 这是正确的吗?

There may be an issue with the current culture. 当前的文化可能存在问题。 So to make sure that you pass the correct format to the parser, instead of ToString(), use some explicit formatting: http://www.csharp-examples.net/string-format-datetime/ 因此,要确保将正确的格式而不是ToString()传递给解析器,请使用一些显式格式: http : //www.csharp-examples.net/string-format-datetime/

I assume this is just a test code and you're going to pass the variable as a string parameter. 我假设这只是一个测试代码,您将把变量作为字符串参数传递。 So you just need to make sure that you pass the value in the correct format. 因此,您只需要确保以正确的格式传递值即可。

UPDATE 更新

Based on the comments bellow, I understand the issue a little bit more. 根据下面的评论,我对该问题有了更多的了解。 You can't declare the date inside the method because it gets updated every time this method is called which is probably every second. 您无法在方法中声明日期,因为该方法每次调用时都会更新,而日期可能是每秒。 If you want to be able to have a countdown for each user. 如果您希望每个用户都有一个倒计时。 Get the date and time when a user request the page with the timer and save it to Session: 获取用户使用计时器请求页面的日期和时间,并将其保存到Session:

Session["NeededByDateTime"] = DateTime.Now.AddHours(2);

And then the method would look like this: 然后该方法将如下所示:

protected void Timer1_Tick(object sender, EventArgs e)
{
    //for the sake of simplicity but you should check if it exists first
    DateTime NeededByDateTime = (DateTime)Session["NeededByDateTime"]; 

    time1 = NeededByDateTime - DateTime.Now;
    if ((time1.Hours == 0) && (time1.Minutes == 0) && (time1.Seconds == 0))
    {
        Label1.Text = "Time Expired!";
    }
    else
    {
        string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til launch.", time1.Days, time1.Hours, time1.Minutes, time1.Seconds);
        Label1.Text = countDown.ToString();
    }

}

Your example won't work, since time1 is always going to be equal to 2 hours every time the Timer calls the Tick method. 您的示例将不起作用,因为每次Timer调用Tick方法时, time1始终等于2小时。

protected void Timer1_Tick(object sender, EventArgs e)
{
    String DateTimeFuture = DateTime.Now.AddHours(2).ToString(); // NOW + 2 HOURS
    DateTime NeededByDateTime = DateTime.Parse(DateTimeFuture);
    TimeSpan time1 = new TimeSpan();
    time1 = NeededByDateTime - DateTime.Now; // NOW + 2 HOURS - NOW = 2 HOURS
    if ((time1.Hours == 0) && (time1.Minutes == 0) && (time1.Seconds == 0))
    {
        Label1.Text = "Time Expired!";
    }
    else
    {
        string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til launch.", time1.Days, time1.Hours, time1.Minutes, time1.Seconds);
        Label1.Text = countDown.ToString();
    }
}

You should declare the NeededByDateTime variable outside the Tick method, so it remains constant and not updated on every tick. 您应该在Tick方法外部声明NeededByDateTime变量,以便它保持不变,并且不会在每个滴答中更新。

private DateTime NeededByDateTime = DateTime.Now.AddHours(2); //.., or whatever you want 

protected void Timer1_Tick(object sender, EventArgs e)
{
    TimeSpan time1 = NeededByDateTime - DateTime.Now;
    if ((time1.Hours == 0) && (time1.Minutes == 0) && (time1.Seconds == 0))
    {
        Label1.Text = "Time Expired!";
    }
    else
    {
        string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til launch.", time1.Days, time1.Hours, time1.Minutes, time1.Seconds);
        Label1.Text = countDown.ToString();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM