简体   繁体   中英

Session Duration in ASP.NET C#

I am new to ASP.NET and I was trying to find the Session Duration from the Page Load time to the Time I click a Button to End the Session. I was trying to use DateTime and TimeSpan, but the problem is that the DateTime value generated in one event cannot be accessed in the other event.

'// 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);

          }
      } 
     }'

You can use Session variable to overcome this problem. You need to set session variable value at the time of begin_Click event is invoked.

 public void begin_Click(object sender, EventArgs e)
{
    DateTime tstart = DateTime.Now;
    SesStart.Text = tstart.ToString();
    Session["BeginEnd"] = tstart;
}

and the time of end_Click is clicked do this

public void end_Click(object sender, EventArgs e)
{
    DateTime tend = DateTime.Now;
    DateTime tstart = Convert.ToDateTime(Session["BeginEnd"]);
    TimeSpan tspan = tend - tstart;
    SesEnd.Text = tend.ToString();
    Dur.Text = Convert.ToString(tstart);
 }

you need to save Start Time in Session

// Button to Start the Session
public void begin_Click(object sender, EventArgs e)
{
    DateTime tstart = DateTime.Now;
    SesStart.Text = tstart.ToString();
    Session["StartTime"] = tStart;
}

and use it in your end_Click

// Button to end the Session
public void end_Click(object sender, EventArgs e)
{
    DateTime tend = DateTime.Now;
    var tstart = Session["StartTime"] as DateTime; // see this                      
    TimeSpan tspan = tend - tstart;
    SesEnd.Text = tend.ToString();
    Dur.Text = Convert.ToString(tstart);
 }

Using Session is the best approach here . Since your page is post back so it will loose any temporary hold values.

  1. on Start Create a Session["time1"] = DateTime.Now;
  2. on Stop retrieve the value from the session DateTime dt = Session["time1"];

Let me know if you need any other clarifications on this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM