我是ASP.NET的新手,我試圖從頁面加載時間到時間點擊按鈕結束會話時查找會話持續時間。我試圖使用DateTime和TimeSpan,但問題是在一個事件中生成的DateTime值無法在其他事件中訪問。在ASP.NET C中的會話持續時間#
'// Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication17
{
public partial class WebForm1 : System.Web.UI.Page
{
//DateTime tstart, tnow, tend;
protected void Page_Load(object sender, EventArgs e)
{
}
// Button to Start the Session
public void begin_Click(object sender, EventArgs e)
{
DateTime tstart = DateTime.Now;
SesStart.Text = tstart.ToString();
}
// To Display the Present Time in UpdatePanel using AJAX Timer
protected void Timer1_Tick(object sender, EventArgs e)
{
DateTime tnow = DateTime.Now;
PresTime.Text = tnow.ToString();
}
// Button to end the Session
public void end_Click(object sender, EventArgs e)
{
DateTime tend = DateTime.Now;
//The Problem exists here. the value of tstart is taken by default as
TimeSpan tspan = tend - tstart;
SesEnd.Text = tend.ToString();
Dur.Text = Convert.ToString(tstart);
}
}
}'