日期:2014-05-18  浏览次数:20779 次

获取系统时间
我想在进入这个页面的时候获取到系统的一个系统时间也就是login_time
然后我答完题点击提交按钮的时候再获取一个系统时间submit_time;
login-time是在protected void Page_Load(object sender, EventArgs e)里面
{ DateTime login_time = DateTime.Now; }
submit_time是在 protected void submit_Click(object sender, EventArgs e)里面
 { DateTime submit_time = DateTime.Now; }
我是等到这个页面过了五分钟左右才点Submi按钮
可是我获取到的俩个时间是一样的,这是为什么啊



------解决方案--------------------
HTML code
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      HiddenField1.Value = DateTime.Now.Ticks.ToString();
    }

  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    DateTime login_time = new DateTime(Convert.ToInt64(HiddenField1.Value));
    DateTime submit_time = DateTime.Now;
    TimeSpan ts = submit_time - login_time;
    Response.Write("时间间隔(秒):" + ts.Seconds.ToString());
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <asp:HiddenField ID="HiddenField1" runat="server" />
  <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
  </form>
</body>
</html>