日期:2014-05-20  浏览次数:20395 次

新手求助:.net操作cookies
思路是这样的:做了三个按钮,点了以后加载不同的CSS文件,并通过cookies来保存用户所选择的CSS信息。现在只是暂时用一个Label控件来显示用户选择了哪个CSS。

问题是:每次页面加载时Label控件上显示的字都不是cookies里所保存的。刷新页面以后得到的才是cookies里的信息。
请问一下程序是哪里错了吗?


public   partial   class   _Default   :   System.Web.UI.Page  
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                if   (Request.Browser.Cookies   ==   true)
                {
                        if   (Request.Cookies[ "myStyle "]   ==   null)
                        {
                                HttpCookie   MyCookie   =   new   HttpCookie( "myStyle ");
                                MyCookie.Value   =   "style1 ";
                                Response.Cookies.Add(MyCookie);
                                this.txtName.Text   =   "you   haven   never   beet   to   this   website. ";
                        }
                        else
                        {
                                this.txtName.Text   =   Request.Cookies[ "myStyle "].Value;
                        }
                }  
        }
        protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                HttpCookie   MyCookie   =   new   HttpCookie( "myStyle ");
                MyCookie.Value   =   "style1 ";
                Response.Cookies.Add(MyCookie);
        }
        protected   void   Button2_Click(object   sender,   EventArgs   e)
        {
                HttpCookie   MyCookie   =   new   HttpCookie( "myStyle ");
                MyCookie.Value   =   "style2 ";
                Response.Cookies.Add(MyCookie);
        }
        protected   void   Button3_Click(object   sender,   EventArgs   e)
        {
                HttpCookie   MyCookie   =   new   HttpCookie( "myStyle ");