Pergunta

Heres the code I have re arranged, but still have same problem, not able to find my start and end times on last page? How do I do this?

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);
    }
}

Now can anyone help me on this? Should I use session, if I should, how do I use it with datetime and timespan, etc. Thanks!

Foi útil?

Solução

Problem : You are declaring your startTime and endTime variables inside the functions as below:

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

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

Solution : Declare your startTime and endTime variables as class variables and then assign the values in the start() and end() functions.

Try This:

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
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top