Count down numbers [closed]
Using C# (no Javascript) I want to display the numbers 10 to 1 one after the other. Each number should be displayed for 10 seconds.
Can anyone help in this.
Regards, Vivek
You could have a meta-refresh for 10 seconds that refresh every 10 seconds. You could put the number in the query string and print it out to the page.
Take a look at the Timer
<asp:ScriptManager runat="server" id="ScriptManager1" />
<asp:Timer ID="Timer1" runat="server" Interval="10000"
OnTick="Timer1_Tick">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1"
EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" ></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
code behind
int counter=1;
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text =1++;
}
for more info: http://msdn.microsoft.com/en-us/library/bb398865.aspx
Without javascript (update panel etc) you could reload the page with a thread sleep delay:
string currentValue = Convert.ToString(Request.QueryString["val"]);
label.Text = currentValue;
if (Convert.ToInt32(currentValue) != 0)
{
currentValue = Convert.ToString(Convert.ToInt32(currentValue) - 1);
Thread.Sleep(10000);
Response.Redirect("Default.aspx?val=" + currentValue);
}
And kick things off by loading the page with a querystring: Default.aspx?val=10
Here:
test.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string t = Request.Params["t"] ?? "10";
string html = "<html><head><meta http-equiv=\"refresh\" content=\"10; url=http://localhost:3687/website/test.aspx?t=NEXT\"><head><body>NOW</body></html>";
html = html.Replace("NOW", t);
int next = (int.Parse(t) - 1);
if (next == 0) next = 1;
html = html.Replace("NEXT", next+"");
Response.ContentType = "text/html";
Response.Write(html);
Response.End();
}
test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
As @Mark Byers suggested, you can use meta refresh for this... and it works. Just change http://localhost:3687/website/test.aspx
in html
var to the URL of your ASPX page.
So you just need to open http://localhost:3687/website/test.aspx
, that will generate the page with the next meta refresh URL and its parameter t
set to the next lower value, and so on until it gets to 1.
精彩评论