protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetLeft.Value = invited.GetInviteCountByWeb().ToString();
HttpCookie oldCookie = Request.Cookies["Time"];
if (oldCookie != null)
{
if (DateTime.Now.ToString("yyyy-MM-dd") == Convert.ToDateTime(oldCookie.Values["GetTime"]).ToString("yyyy-MM-dd"))
{
this.IsGet.Value = "false";
}
else
{
HttpCookie newCookie = new HttpCookie("Time");
newCookie.Values.Add("GetTime", DateTime.Now.Date.ToString("yyyy-MM-dd"));
newCookie.Expires = DateTime.Now.AddHours(24.0);
Response.Cookies.Add(newCookie);
}
}
}
}
但是它不起作用,每次关闭浏览器时oldcookie都是空的.. 那么我如何设置按钮每天点击一次?
最佳答案
你的else语句位置错误,试试这样;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetLeft.Value = invited.GetInviteCountByWeb().ToString();
HttpCookie oldCookie = Request.Cookies["Time"];
if (oldCookie != null)
{
if (DateTime.Now.ToString("yyyy-MM-dd") == Convert.ToDateTime(oldCookie.Values["GetTime"]).ToString("yyyy-MM-dd"))
{
this.IsGet.Value = "false";
}
}
else
{
HttpCookie newCookie = new HttpCookie("Time");
newCookie.Values.Add("GetTime", DateTime.Now.Date.ToString("yyyy-MM-dd"));
newCookie.Expires = DateTime.Now.AddHours(24.0);
Response.Cookies.Add(newCookie);
}
}
关于C# 按钮每天点击一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11132372/