I'm building a MVC3 app which has a feature that requires me to measure the time that each user takes to complete a certain task.
I've implemented similar functions before in other languages (Perl), but not sure how to do this in ASP.NET and C#.
What I want to do is:
1) When an user clicks on "Start" button, I get the system time then, assign it to "StartTime";
2) When the user completes, and click on "Submit" button (which could be 5-6 pages later, or on the same page), I get the system time again, assign it to "EndTime";
3) I then get the difference and assign it to the var I want: int TimeTaken = EndTime - StartTime; and convert it to minutes or seconds for display.
Please let me know how should I implement this in C#; sample code is appreciated!
Session is available from within any Ation method.
Then you configure the sessions for the maximum time you would like to wait the user provide an answer to a question. If this time expires session data are disposed: when you detect this (Session returning null) you show a messaqge sayng maximum time is
expired.
Thanks for the reply - what you suggested makes great sense. I tried your suggestion and read a few MSDN articles on sessionState to get more info. Here is where I am (keep in mind I have never used sessions in my ASP.NET app, even though I used session
variables in Perl and classic ASP before so I understand the concept):
1) I added the following namespaces in my controller.cs - are they enough? or am I still missing some needed namespace?
using System.Web.Configuration; using System.Web.SessionState; using System.Web.SessionState.HttpSessionState;
2) I added the following in my web.config, under "<system.web>...</system.web>":
3) I added this line of code you suggested (Session("StartTime") = DateTime.Now() ;) in my action where it should be, but the VS shows a red line under "Session", saying:
"The name 'Session' does not exist in the current context."
I searched around but have not found the solution. Can you provide more details or sample code to address this?
claudia888
Member
181 Points
435 Posts
How to associate the timer function with a Start and Stop button?
Apr 28, 2012 07:33 PM|LINK
HI,
I'm building a MVC3 app which has a feature that requires me to measure the time that each user takes to complete a certain task.
I've implemented similar functions before in other languages (Perl), but not sure how to do this in ASP.NET and C#.
What I want to do is:
1) When an user clicks on "Start" button, I get the system time then, assign it to "StartTime";
2) When the user completes, and click on "Submit" button (which could be 5-6 pages later, or on the same page), I get the system time again, assign it to "EndTime";
3) I then get the difference and assign it to the var I want: int TimeTaken = EndTime - StartTime; and convert it to minutes or seconds for display.
Please let me know how should I implement this in C#; sample code is appreciated!
Thanks,
Claudia
francesco ab...
All-Star
20912 Points
3279 Posts
Re: How to associate the timer function with a Start and Stop button?
Apr 29, 2012 12:15 PM|LINK
You cqan store them in the session data:
Session("StartTime") = DateTime.Now() ;
Session is available from within any Ation method.
Then you configure the sessions for the maximum time you would like to wait the user provide an answer to a question. If this time expires session data are disposed: when you detect this (Session returning null) you show a messaqge sayng maximum time is expired.
Mvc Controls Toolkit | Data Moving Plug-in Videos
claudia888
Member
181 Points
435 Posts
Re: How to associate the timer function with a Start and Stop button?
Apr 29, 2012 04:46 PM|LINK
Hi Francesco,
Thanks for the reply - what you suggested makes great sense. I tried your suggestion and read a few MSDN articles on sessionState to get more info. Here is where I am (keep in mind I have never used sessions in my ASP.NET app, even though I used session variables in Perl and classic ASP before so I understand the concept):
1) I added the following namespaces in my controller.cs - are they enough? or am I still missing some needed namespace?
using System.Web.Configuration;
using System.Web.SessionState;
using System.Web.SessionState.HttpSessionState;
2) I added the following in my web.config, under "<system.web>...</system.web>":
<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
stateNetworkTimeout="10"
sqlCommandTimeout="30"
customProvider=""
cookieless="UseCookies"
cookieName="ASP.NET_SessionId"
timeout="20"
allowCustomSqlDatabase="false"
regenerateExpiredSessionId="true"
partitionResolverType=""
useHostingIdentity="true">
<providers>
<clear />
</providers>
</sessionState>
3) I added this line of code you suggested (Session("StartTime") = DateTime.Now() ;) in my action where it should be, but the VS shows a red line under "Session", saying:
"The name 'Session' does not exist in the current context."
I searched around but have not found the solution. Can you provide more details or sample code to address this?
Thanks,
Claudia
francesco ab...
All-Star
20912 Points
3279 Posts
Re: How to associate the timer function with a Start and Stop button?
Apr 29, 2012 05:03 PM|LINK
Within any action method....
but it is Session["StartTime"] not Session("StartTime")....I wrote wrong parentheses..
Mvc Controls Toolkit | Data Moving Plug-in Videos
claudia888
Member
181 Points
435 Posts
Re: How to associate the timer function with a Start and Stop button?
Apr 29, 2012 05:16 PM|LINK
Hi,
I just found that out as well, it's [ ], not ( ) -- thanks!
This worked:
Session["StartTime"] = DateTime.Now;
Thanks again!
Claudia