【问题标题】:TimeSpan still not working时间跨度仍然无法正常工作
【发布时间】:2014-03-10 21:27:21
【问题描述】:

这是我重新整理的代码,但仍然有同样的问题,无法在最后一页找到我的开始和结束时间?我该怎么做?

public void start()
    {
        DateTime startTime = DateTime.Now;
    }

    protected void btnStart_Click(object sender, EventArgs e)
    {
        start();
        Response.Redirect("~/end.aspx");
    }

public void end()
    {
        DateTime endTime = DateTime.Now;
    }
    protected void btnEnd_Click(object sender, EventArgs e)
    {
        end();
        Response.Redirect("~/display.aspx");
    }

public partial class display : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TimeSpan timeSpent = endTime - startTime;

        lblDisplay.Text = string.Format("Time: {0}", timeSpent);
    }
}

现在有人可以帮我解决这个问题吗?我应该使用会话,如果应该,如何将它与日期时间和时间跨度等一起使用。谢谢!

【问题讨论】:

  • 您正在为 startTimeendTime 声明 local 变量...它们仅适用于您声明它们的方法。
  • 好的,谢谢,我要在课堂上制作它们,看看我能不能得到它!谢谢@JonSkeet

标签: c# asp.net webforms session-state timespan


【解决方案1】:

问题:您在函数中声明了startTimeendTime 变量,如下所示:

public void start()
{
    DateTime startTime = DateTime.Now; //remove the declartion from here
}

public void end()
{
    DateTime startTime = DateTime.Now; //remove the declaration from here
}

解决方案:startTimeendTime 变量声明为类变量,然后在start()end() 函数中赋值。

试试这个:

DateTime startTime; //declare here
DateTime endTime;   //declare here

public void start()
{
    startTime = DateTime.Now; //assign value here
}
public void end()
{
    endTime = DateTime.Now;  //assign value here
}

【讨论】:

  • @LastKingIsKingin:不客气,请不要忘记接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 2012-04-18
  • 2016-01-18
  • 2014-12-10
  • 2015-06-17
相关资源
最近更新 更多