学习ASP.NET中的Application、Session、Cookie

学习ASP.NET中的Application、Session、Cookie

1.Application建立的变量,在系统内部任何地方都可以访问,通常网站地访问统计可能会用的较多。如果要用到Application首先在VS2005中建立一个global.asa文件。例如我们要写一个网站访问数量的统计的话,在global.asa中先声明变量iCount。如下所示:

       protected void Application_Start(object sender, EventArgs e)
        {
            Application["iCount"] = 0;
        }


然后在Page_Load事件中打入以下代码:

       protected void Page_Load(object sender, EventArgs e)
        {
            Application["iCount"] = (int)Application["iCount"] + 1;
            Response.Write(Application["iCount"]);
        }

  运行后就可以看到计数,每刷新一次计数会自动累加。如果要停止累加的话,只能把IIS停止后再启动,此时计数会自动归零

 

由于Application变量是一个共享的对象,所以有多人来访问它的时候我们就必须用到锁定。当某一个用户需要操作它的时候就用lock锁定它,操作完成后用unlock来解锁。代码如下:

        protected void Page_Load(object sender, EventArgs e)
        {
            Application.Lock;
            Application["iCount"] = (int)Application["iCount"] + 1;
            Application.UnLock;

            Response.Write(Application["iCount"]);

        }

 

2. Session是每个用户使用程序时就会建立对象,也就是Application对象只有一个,而Session对象有很多个。Session不是共享的,当用户退出程序后它就会在20分钟后销毁,当然,我们可以设定这个销毁的时间。当用户再次进入程序是Session对象还没有被销毁的话,我们就可以继续使用它们,否则就要再建立了。Session 的优点是它在 Web 服务器上保持用户的状态信息,可供在任何时间从任何页访问这些信息。因为浏览器并不需要存储任何这些信息,所以可以使用任何浏览器,甚至可以使用 PDA 或手机这样的浏览器设备。

  首先,我们要建立一个Session变量

      protected void Session_Start(object sender, EventArgs e)
        {
            Session["UserName"] = "你的姓名";

            //也可以是以下的方式建立

           //Session.Add("UserName","你的姓名");
        }

 调用的时候

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<br>" + Session["UserName"]);
            Response.Write("<br>" + Session["PSW"]);
        }
           

 ASP.NET中Session的用法之一:使用Session设定权限

Session的初次使用:
protected void Page_Load(object sender, EventArgs e)
{//这是页面的初始化
     if (!Page.IsPostBack)
     {//判断是否为初次执行
         if (Object.Equals(Session["AdminName"], null))
         {//判断在Session["AdminName"]是否存在值
             Response.Redirect("ErrorPage.aspx", true);
         }
         else
         {//要是存在则记录下这个人的用户名
            Name.Text =  Session["AdminName"].ToString();
         }
     }
}

ASP.NET中Session的用法之二:进行页面传值

使用Session变量传值是一种最常见的方式了,此种方式不仅可以把值传递到下一个页面,还可以交叉传递到多个页面,直至把Session变量的值Remove后,才消失。

<1>.创建一个页面,

<2>.在页面内添加两个TextBox,叫txtName,txtAddr,添加一个Button,叫btnConfirm

  protected void btnConfirm_Click(object sender, EventArgs e)
    {
        Session["Name"] = txtName.Text;
        Session["Addr"] = txtAddr.Text;
        Response.Redirect("Other.aspx");

    }

<3>.再创建一个页面,叫Other.aspx
<4>.在页面内添加两个Label,叫 lblName,lblAddr

   protected void Page_Load(object sender, EventArgs e)
    {
        lblName.Text = Session["Name"].ToString();
        lbladdr.Text = Session["Addr"].ToString();

        Session.Remove("Name");
        Session.Remove("Addr");
    }


 3. Cookie 其实就是在客户本机上建立一个对象,它可以长时间保存客户的一些信息。例如我们浏览网页是经常看见的记住我,或者论坛所说的保存一个月等等

   〈1〉写入Cookie

     protected void btnConfirm_Click(object sender, EventArgs e)
     {
        HttpCookie myCK = new HttpCookie("UserInfo");
        myCK.Values["Name"] = txtName.Text;
        myCK.Values["Psw"] = txtPsw.Text;
        //设置此Cookie在一天后过期
        myCK.Expires = DateTime.Now.AddDays(1);
        Response.Cookies.Add(myCK);
      }

     <2>读取Cookie

    protected void BtnGetCookie_Click(object sender, EventArgs e)
    {
        HttpCookie getCK = Request.Cookies["UserInfo"];
        if (Request.Cookies["UserInfo"]!=null)
        {
            Response.Write(getCK.Values["Name"]);
            Response.Write("<br>" + getCK.Values["Psw"]);
        }

    }
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章