Click to See Complete Forum and Search --> : Timers in ASP.NET using C#
sabareeswaren
03-27-2006, 05:29 AM
Hi,
i am doing an inhouse project titled "On-Line Quiz" in asp.net using c#. in that, i need to know how can i run the seconds?
I allocate 180 seconds i.e 3 minutes for every questions. if anyone agree to write the test, the timer should get start. for every 180 seconds a new question should be fire out.
I want to do "PAUSE" option which stops the timer and get started while i click the "CONTINUE" button.
I dont know "Timers" in asp.net. please could anyone help me to complete this.
Thanks in advance!!!!!!1
takkie
03-27-2006, 02:54 PM
create a timer object, and set the interval property of the timer object. Then call the start method.
Once it is started, it will get into the elapse method in the interval number of seconds.
i dont remember the syntax, but something like this..
timer1.interval = 10000;
response.write("calling start");
timer1.start();
response.write("start is callled...it will start in 10 seconds...");
void timer1_elapse(whatever obj, whatever args)
{
response.write("It has been 10 seconds, firing now");
timer1.stop(); // will stop the timer, so it will not execute every 10 seconds.
}
-tak
sabareeswaren
03-28-2006, 01:48 AM
Sir,
Its working correctly. i want to know that, how can i display a new page while clicking a button say (WebForm1.aspx.cs), a page named (WebForm1.aspx.cs) should open? How can i do that? Please Help me.....
Thanks in advance.....
takkie
03-29-2006, 01:34 PM
Sir,
Its working correctly. i want to know that, how can i display a new page while clicking a button say (WebForm1.aspx.cs), a page named (WebForm1.aspx.cs) should open? How can i do that? Please Help me.....
Thanks in advance.....
You mean, just redirect the user to a new page?
void button_click(obj sender, eventargs a)
{
response.redirect("theNewPage.aspx");
}
like this?
-tak
sabareeswaren
03-30-2006, 01:56 AM
Hi,
Please anyone give me a sample code for incrementing a counter for every second in asp.net using c#. I dont know anything about Timers. please reply me........
Thanks in advance!!!!!
Your's Truly,
Sabareeswaren
handshakeit
03-30-2006, 05:13 AM
follwing is some example
handshakeit
03-30-2006, 05:15 AM
Hi
I think there is no timer control in ASP.net
If you want to calculate the time spent by client on your page you are to use script function here is an example in vb.net
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
Dim strTimePageLoaded As String
strTimePageLoaded = Request.QueryString("time")
If IsDate(strTimePageLoaded) Then
lblTimeSpent.Text = "You spent " _
& DateDiff("s", CDate(strTimePageLoaded), Now()) _
& " seconds looking at the previous page."
Else
lblTimeSpent.Text = "You haven't clicked on the link below yet."
End If
End Sub
</script>
<html>
<head>
<title>ASP.NET Time Spent on Page Sample</title>
</head>
<body>
<p>
<asp:Label id="lblTimeSpent" runat="server" />
</p>
<p>
<a href="time.aspx?time=<%= Server.URLEncode(Now())%>">How long have I spent on this page?</a>
</p>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/time_aspx.asp">here</a> to read about and download the source code.
</p>
</body>
</html>
Hope this code is helpful for you
Regards
Abhi
Handshakeit (http://www.handshakeit.com)
sabareeswaren
04-03-2006, 01:40 AM
Sir,
We are doing this only in Asp.net using c# sir. My friends are telling that timer is possible using SESSIONS. can you please give me the solution.... i have no hope in that field. so please do help me....
Tnaks in Advance!!!!!
handshakeit
04-03-2006, 01:58 AM
hi
for this change your global.asax file to the project and then added then changed the Application_Start method, added a static varible and a timer event. Here is what I added:
public static System.Timers.Timer timer = new System.Timers.Timer();
void Application_Start(object sender, EventArgs e)
{
Application.Add("Counter", 0);
timer.Interval = 5000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
int x = (int)Application["Counter"];
x++;
Application["Counter"] = x;
}
Then in the code behind of the default.aspx page I did this:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Application["Counter"].ToString();
}
Hope this will help
sabareeswaren
04-03-2006, 02:57 AM
sir,
Its working but, At first time the Label shows only "1". for knowing the value, i have to press the refresh button each time. how can i get a dynamically changing value in the Label for each second? please help me.......
Thanks in Advance!!!
Sabareeswaren.
handshakeit
04-03-2006, 03:09 AM
in the code i have set the interval equal to 5 sec
you can set it to 1 second in following code
void Application_Start(object sender, EventArgs e)
{
Application.Add("Counter", 0);
timer.Interval = 5000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
replace the following line
timer.Interval = 5000;
with
timer.Interval = 1000;
sabareeswaren
04-03-2006, 03:29 AM
sir,
You didnt get me sir! i already set the Interval to 1000. Whats my Problem is, if i run the program the label shows only the value "1". if i press F5 only the value of the label get changed. How can i make this as Dynamic one as in visual basic? please help me......
Thanks in Advance!!!!!!
Sabareeswaren.
handshakeit
04-04-2006, 12:44 AM
as in visual basic?
how you do this in VB.....
do you mean VB.Net if so give some code of it
in same way it will be in C#
sabareeswaren
04-04-2006, 01:20 AM
Hi,
No sir, i mean in VB.Net only but there "timer_tick" is there but not in asp.net. The code that you gave yesterday works well but its not dynamic. If we click the F5 button only we are able to get the updated seconds. i need that Seconds should be displayed at runtime.. Please do help me......
thanks in advance!!!!!
Sabareeswaren
nannish
12-03-2007, 06:29 AM
Hi,
Please anyone give me a sample code for incrementing a counter for every second in asp.net using c#. I dont know anything about Timers. please reply me........
Thanks in advance!!!!!
Your's Truly,
Sabareeswaren
ok