当前位置: 代码迷 >> ASP.NET >> 怎么用Cookies保存主题信息
  详细解决方案

怎么用Cookies保存主题信息

热度:751   发布时间:2013-02-25 00:00:00.0
如何用Cookies保存主题信息?
我在登陆页面设了个选择主题的下拉框,预设了两个主题,分别是蓝色和红色,根据我下拉框的选项时时跟换主题
代码是这样子的
C# code
 protected void Page_Preinit(object sender,EventArgs e)     {        HttpCookie theme = Request.Cookies["Theme"];        if (theme == null)        {            this.Theme = "blue";        }        else        {            this.Theme = theme.Value;        }      }    protected void Page_Load(object sender, EventArgs e)    {        if(!IsCallback)        {            HttpCookie theme = Request.Cookies["Theme"];            if (theme != null)            {                string themestring = theme.Value;                for (int i = 0; i < this.DropDownList1.Items.Count; i++)                {                    if (this.DropDownList1.Items[i].Value == themestring)                    {                        this.DropDownList1.SelectedIndex = i;                    }                }            }        }    }    protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)    {        Response.Cookies["Theme"].Value = this.DropDownList1.SelectedValue;        Response.Redirect("login.aspx");    }


  第一次页面加载的时候。因为Request.Cookies["Theme"];为空,所以主题是蓝色,然后我在下拉框内选择红色的时候,方法还是会先经过Page_Preinit(object sender, EventArgs e)这时Request.Cookies["Theme"];依然为空。然后到Page_Load(object sender, EventArgs e)再到DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)我在这存了一个Cookies,页面重新加载。主题会变为红色,
  问题来了,当我再选择蓝色的时候,主题依然会是红色,不会变。这怎么解决啊?

------解决方案--------------------------------------------------------
if (!Page.IsPostBack)
{
BindGrid();
}
这样就不会再次执行了
  相关解决方案