我需要将两个日期时间的差异精确到毫秒比较,第一个日期时间将小于第二个,以停止 gridviews 删除事件中的循环
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (!Ok2Delete(e.RowIndex)) return;
// your logic goes here and the above IF statement
// will hopefully guarantee your code to run once.
}
private bool Ok2Delete(int ri) // ri is the record index to be deleted
{
if (Session["ri"] == null ||
(!((ri == ((int)Session["ri"])) &&
(DateTime.Now.Subtract((DateTime)Session["ri_time_stamp"]).Seconds < 2))))
{
Session["ri"] = ri;
Session["ri_time_stamp"] = DateTime.Now;
return true;
}
return false;
}
此代码未按预期工作
请您参考如下方法:
使用 .TotalSeconds...否则 122 TotalSeconds 的 TimeSpan 将是 2 秒。
private bool Ok2Delete(int ri) // ri is the record index to be deleted
{
if (Session["ri"] == null ||
(!((ri == ((int)Session["ri"])) &&
(DateTime.Now.Subtract((DateTime)Session["ri_time_stamp"]).TotalSeconds < 2))))
{
Session["ri"] = ri;
Session["ri_time_stamp"] = DateTime.Now;
return true;
}
return false;
}
