【发布时间】:2016-06-20 12:21:19
【问题描述】:
我是 ASP.NET 的新手,我试图找到从页面加载时间到单击按钮结束会话的时间的会话持续时间。我尝试使用 DateTime 和 TimeSpan,但问题是在一个事件中生成的 DateTime 值无法在另一个事件中访问。
'// 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);
}
}
}'
【问题讨论】: