简体   繁体   English

1滴答后,ASP.Net(语言C#)中的计时器停止

[英]Timer in ASP.Net (Language C#) stops after 1 tick

i wanted to use a timer in a program i'm working on, but it always stops after 1 tick !! 我想在我正在开发的程序中使用计时器,但是它总是在1个滴答后停止! can you give me any tips to make it repeat unstoppably (or until i want it to) please ?? 你能给我任何提示,以使其不断地重复(或者直到我想要)吗?
this is my code: 这是我的代码:

    int i = 20;

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "(20)";
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        i--;
        Label1.Text = "(" + i + ")";

        if (i == 0)
        {
            Session["Profession"] = "Visiteur";
            Response.Redirect("Acceuil.aspx");
        }

Edit: my HTML code : 编辑:我的HTML代码:

<form id="form1" runat="server">
<div><center>
    <span class="style1">message</span><br 
        class="style1" />

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>

    <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick" >
    </asp:Timer>

        <asp:Timer ID="Timer2" runat="server" Interval="1000">
    </asp:Timer>

        <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
    </asp:UpdatePanel>

    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" 
        PostBackUrl="~/Acceuil.aspx">Retour</asp:LinkButton>
    <br />
        </center>
</div>
</form>

Edit2: what i want to do is the normal redirecting code, count to 20 and refreshing every second (to show to user how much secs left) and at the end it redirects to a new page, but it wasn't working, so i was wondering why ?? Edit2:我要执行的操作是正常的重定向代码,计数到20并每秒刷新一次(向用户显示还剩多少秒),最后它重定向到新页面,但是没有工作,所以我想知道为什么? and i think i got my answer from @Andrei Rînea, and thank you everyone for your help : ) 我想我从@AndreiRînea得到了我的答复,并感谢大家的帮助:)

Edit3: Solved and here is the code : Edit3:解决了,这里是代码:

static int i = 20;
protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    Label1.Text = "20";
}
protected void Timer1_Tick(object sender, EventArgs e)
{

    i--;
    Label1.Text = i.ToString();

    if (i == 0)
    {
        i = 20;
        Session["Profession"] = "Visiteur";
        Response.Redirect("Acceuil.aspx");
    }
}

Thanks everyone again : ) 再次感谢大家:)

I'm going to assume here that you are wishing to have a process that ticks away on your server in the background. 我在这里假设您希望在后台的服务器上创建一个进程。

I'm also going to assume you've tried to add this to your Webform. 我还要假设您已尝试将其添加到Webform中。

If so, the issue you have encountered, is that your Webform object only exists for the short time that it is processing your request, after which it is disposed of - including your timer. 如果是这样,则您遇到的问题是,Webform对象仅在处理请求的短时间内存在,之后将被丢弃-包括计时器。

If I'm correct, you'd probably like to take a look at Quartz.Net : 如果我是对的,那么您可能想看看Quartz.Net

http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net

You are trying to run client side code on the server. 您正在尝试在服务器上运行客户端代码。 You need to do this in JavaScript instead. 您需要用JavaScript代替。

The C# code will be run just before rendering the page and not longer. C#代码将在呈现页面之前运行,并且不会更长。 You probably believe the code will run as long as the visitor is on the page which is not the case. 您可能认为只要访问者在页面上,代码就可以运行,事实并非如此。

Please use the following code :



    static int i = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        Label1.Text = "(20)";
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {

        i--;
        Label1.Text =i.ToString();

        if (i == 0)
        {
            Session["Profession"] = "Visiteur";
            Response.Redirect("Acceuil.aspx");
        }
    }


    your HTML code should be like :

    <form id="form1" runat="server">
        <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">

            </asp:ScriptManager>


            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>



                 <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="1000">
                   </asp:Timer>
                </ContentTemplate>

            </asp:UpdatePanel>

        </div>
        </form>

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

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