【问题标题】:Session Duration in ASP.NET C#ASP.NET C# 中的会话持续时间
【发布时间】: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);

          }
      } 
     }'

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以使用 Session 变量来解决这个问题。您需要在调用 begin_Click 事件时设置会话变量值。

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

    并且点击 end_Click 的时间这样做

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

    【讨论】:

    • 为什么要将它存储为字符串,特别是如果您在检索它后要做的第一件事是将其转换回 DateTime?
    【解决方案2】:

    您需要在会话中保存开始时间

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

    并在您的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);
     }
    

    【讨论】:

      【解决方案3】:

      在这里使用 Session 是最好的方法。由于您的页面已回发,因此它将丢失任何临时保留值。

      1. 在开始创建会话["time1"] = DateTime.Now;
      2. 在停止时从会话中检索值 DateTime dt = Session["time1"];

      如果您需要对此进行任何其他说明,请告诉我。

      【讨论】:

        猜你喜欢
        • 2012-03-02
        • 2018-01-24
        • 2018-09-28
        • 1970-01-01
        • 2017-06-20
        • 1970-01-01
        • 2014-05-22
        • 1970-01-01
        • 2017-03-22
        相关资源
        最近更新 更多