3

I don't have any problem regarding the code, its working fine. But, I am looking for an alternate method for this count down.

Here is the code

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> 

Code behind file

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");
            }
        }
    }
}

All I am looking for is any alternate method.

shanu
  • 61
  • 1
  • 2
  • 9
  • Why are you looking for an alternative method? What else have you tried? – Richard Oct 08 '12 at 17:00
  • You may try using a jQuery plugin. – ebram khalil Oct 08 '12 at 17:02
  • I have tried count down timer using java script but same with lots of code. Reason for an alternate method is " i don't want to use Ajax extension". if its possible will you guys please help me out. – shanu Oct 08 '12 at 17:04

2 Answers2

2

Unless you need to specify redirect location dynamically through c# code, you could implement a purely javascript client side method. It can be a simple javascript function which will cause the window to navigate to desired page.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
2

If your timer does not need anything from the server side than you should probably just do a timer on the client side using JQuery or plain Javascript - it will keep the client-server communication low (0).

If you do have something happening on your backed that you have to check I think you should still change it to some JQuery/Javascript + some web service or web method or simple ashx handler. Although what you presented does work I think it is cleaner to keep this outside of the page functionality.

There are a lot of client side timers: http://code.cyntaxtech.com/plugins/jquery-timer/demo or this jQuery countdown timer

Community
  • 1
  • 1
Bogdan
  • 1,323
  • 15
  • 15
  • This code is only client side and nothing to do with server or backend. Then i should probably switch it to javascript. Thanks for your suggestion. – shanu Oct 08 '12 at 17:24