全速加载中

asp.net学习笔记·Cookie

  •        Cookie是和站点相关的,每次向服务器请求的时候,除了发送表单参数外,还会将和站点相关的Cookie值都提交给服务器,并且将服务器返回的Cookie更新到浏览器Cookie中去,且这是强制性的;
  •      Cookie的生命周期可以设置;
  •        缺点:Cookie不能存储过多的数据;
  •        网站优化案例:网站图片服务器和主站服务器域名不一样,降低提交Cookie消耗的流量;
  •        变量自增代码示例:
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class 变量自增 : System.Web.UI.Page
    {
        private int a = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Response.SetCookie(new HttpCookie("num", "0"));
    //新建HttpCookie对象
                this.Label1.Text = "0";
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
              int.TryParse(Request.Cookies["num"].Value, out a);
                a++;
                Response.SetCookie(new HttpCookie("num", a.ToString()));
                this.Label1.Text = a.ToString();
           
        }
    }
    

      

posted @ 2012-12-22 19:44  许鸿飞  阅读(161)  评论(0编辑  收藏  举报