Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关于asp.net C中的倒计时#_C#_Countdown - Fatal编程技术网

C# 关于asp.net C中的倒计时#

C# 关于asp.net C中的倒计时#,c#,countdown,C#,Countdown,我对代码没有任何问题,它工作正常。但是,我正在寻找另一种倒计时方法 这是密码 CurrentPage.aspx <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div>

我对代码没有任何问题,它工作正常。但是,我正在寻找另一种倒计时方法

这是密码

CurrentPage.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label ID="Label2" runat="server" Text="You will be redirected in..."></asp:Label>
            <asp:Timer ID="Timer1" runat="server">
            </asp:Timer>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Label ID="Label1" runat="server" Text="10"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html> 

代码隐藏文件

using System;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Timer1.Enabled = true;
            Timer1.Interval = 1000;
            Timer1.Tick += new EventHandler<EventArgs>(Timer1_Tick);
        }
        void Timer1_Tick(object sender, EventArgs e)
        {
            int i=(Convert.ToInt16(Label1.Text));
            i = i - 1;
            Label1.Text = i.ToString();
            if (i<0)
            {
                Timer1.Enabled = false;
                Response.Redirect("TargetPage.aspx");
            }
        }
    }
}
使用系统;
命名空间WebApplication1
{
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
Timer1.Enabled=true;
计时器1。间隔=1000;
Timer1.Tick+=新事件处理程序(Timer1\u Tick);
}
无效计时器1_刻度(对象发送方,事件参数e)
{
inti=(Convert.ToInt16(Label1.Text));
i=i-1;
Label1.Text=i.ToString();

如果(i除非您需要通过c#代码动态指定重定向位置,否则您可以实现纯javascript客户端方法。它可以是一个简单的javascript函数,将导致窗口导航到所需页面。

如果您的计时器不需要服务器端的任何东西,那么您可能只需要在cl上执行计时器客户端使用JQuery或纯Javascript-它将保持客户端与服务器之间的低通信(0)

如果你确实有什么事情发生在你的背上,你必须检查一下,我认为你还是应该把它改成一些JQuery/Javascript+一些web服务或web方法或简单的ashx处理程序。虽然你所介绍的确实有效,但我认为把它保留在页面功能之外更干净


有很多客户端计时器:或者这是为什么您要寻找替代方法?您还尝试了什么?您可以尝试使用jQuery插件。我尝试过使用java脚本倒计时,但与许多代码相同。使用替代方法的原因是“我不想使用Ajax扩展”。如果可能的话,你们能帮我一下吗。这段代码只是客户端代码,与服务器或后端无关。然后我可能会将其切换到javascript。谢谢你们的建议。