C#按钮每天点击一次

时间:2012-06-21 06:01:41

标签: c# asp.net

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为null。 那么如何每天设置一次按钮?

1 个答案:

答案 0 :(得分:3)

你的其他陈述在错误的地方,试试这样;

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