我知道這是一個老的文章,但沒有正確的答案,我需要這樣回答。
我已經使用了Ashwin的答案,並使其工作!
您需要創建會話[「SessionStart」]變量,您可以隨時開始會話並開始會話。從那裏它是非常簡單的,我返回的秒作爲值,所以我可以使用javascript來改變秒爲hh:mm:ss,我還包括一個string.format將秒轉換成hh:mm:ss。
// Get the session in minutes and seconds
HttpSessionState session = HttpContext.Current.Session;//Return current session
DateTime? sessionStart = session["SessionStart"] as DateTime?;//Get DateTime object SessionStart
//Check if session has not expired
if (sessionStart.HasValue)
{
TimeSpan timepassed = DateTime.Now - sessionStart.Value;//Get the time passed since the session was created
int TimeOut = (session.Timeout * 60) - Convert.ToInt32(remaining.TotalSeconds);
int h = (TimeOut/3600);
int m = ((TimeOut/60) % 60);
int s = TimeOut % 60;
SessionTimeoutValue.InnerText += TimeOut; // or use the string.format below.
//string.Format("{0}:{1}:{2}",
// h,
// m,
// s);
}
每次頁面加載時會不會重置會話超時?每當您有機會檢查剩餘時間時,它將剛剛重置爲100. – josh3736 2012-07-11 04:13:23