ASP.NET - timer event - Asked By hiren dhameliya on 14-Apr-10 02:35 AM

hi
how i can  use timer in my asp.net application 
ex 
 x= 40 
 timer will  start  0  to 40 second plz reply me as soon as 
with coding exmple
 some explian plz
 
Santhosh N replied to hiren dhameliya on 14-Apr-10 02:39 AM
There is no timer control in asp.net and alternatively you can rely on client side scripts for this...
you can check here for a sample code in client side javascript..
http://stackoverflow.com/questions/109064/countdown-timer-on-asp-net-page
Anand Malli replied to hiren dhameliya on 14-Apr-10 02:55 AM
hi hiren

you can surely use timer at server side aith the help of SCRIPTMANAGER,UPDATEPANEL and TIMER control

put script manager on to your page under that take update panel and in that take timer control provided in toolbox

so your code would be something like below

public partial class _Default : System.Web.UI.Page 
{
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Label1.Text = "Timer: " +timerVar;
        timerVar++;  
    }
}

its easy right...

alternatively client side script is always there,you can use setTimeout or setInterval function for that

thxs
Anoop S replied to hiren dhameliya on 14-Apr-10 03:00 AM
You can use Ajax timer control with ASP.net,

eg for 40sec

<html dir="ltr" xmlns="<a rel="nofollow" target="_new" href="http://www.w3.org/1999/xhtml">">www.w3.org/.../xhtml"></a>

<script runat="server">

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)

Response.Redirect("~/Default.aspx")

End Sub

</script>

<body>

<form id="form1" runat="server">

<h1>Submission successful!</h1>

Thank you for your submission

If you are not redirected in 40 seconds, click <asp:HyperLink runat="server" ID="lnkredirect" NavigateUrl="~/Default.aspx" Text="here"></asp:HyperLink>

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

</asp:ScriptManager>

<asp:Timer ID="Timer1" runat="server" EnableViewState="false" Interval="5000" OnTick="Timer1_Tick">

</asp:Timer>

</form>

</body>

</html>



you can read more from here
http://www.asp.net/AJAX/Documentation/Live/tutorials/TimerControlWithUpdatePanelsTutorial.aspx
Web Star replied to hiren dhameliya on 14-Apr-10 03:23 AM
u just put timer control on page and than u implement these code in code behind file
protected System.Timers.Timer timer1;
private int test;

protected void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
test = 1;

timer1.Interval = 4000;
timer1.Start();

timer1.Enabled = true;

}



private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{

test = test+1;

lblCounter.Text = "count:"+test;

}





In C# the 'timer1.enabled' method calls the 'timer1_Tick' which, as far as i know, is the same as the Elapsed method above.

Many thanks!!!!