滨_Notepad

学习工作点滴积累
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用.net 2.0换肤

Posted on 2007-12-01 21:32  新人~  阅读(169)  评论(0编辑  收藏  举报
换肤,在朋友的提醒下,用cookies。
便想到了用javascript 和cookies 一起来使用,但是javascript 不是太好用。后来就放弃了。
.net 2.0集成有skin 外观文件。可以用skin 和cookies 一起使用来实现这一效果.
方法:
1:)定义外观文件blue 和orage 两个,自动放在App_Themes 文件夹下.
2:)在blue和orange 文件夹下,可以建css文件和.skin文件.
3:)在page_PreInit()根据传参的不同,更改言主题;具体代码如下:
protected void Page_PreInit(object sender,System.EventArgs e)
 {
  string 
str="";
  
if(HttpContext.Current.Request.Cookies["theme"]!=null)
   {
    
str=Convert.ToString(HttpContext.Current.Request.Cookies["theme"])
   }
  
if(!string.IsNullOrEmpty(Request.QueryString["theme"]))
  {
   Page.Theme
=Request.QueryString["theme"];
   HttpCookie skin
=new HttpCookie("theme");
   skin.Value
=Page.Theme;
   Response.Cookies.
Add(skin);
  }
  
else if(str.ToString()=="")
  {
   Page.Theme
="blue";
  }
  
else
  {
   Page.Theme
=Request.Cookies["theme"].Value.ToString();
  }
 }

代码这样写就实现了换肤的功能,但是你关闭该页面,再次打开时还是原来的,不是你更改过的,这是为什么呢?
找了好长时间才知道原来是cookies 没有设有效日期。
在建立cookies时加一句
skin.Expires=convert.ToDateTime(DateTime.Now+TimeSpan.FromDays(1));这样可以设cookies的有效时间为1天
通过这次学习学会了
1 使用cookies
2 skin
3 不旦 在Dream 下可以创建下拉菜单 ,在FireWorks 也可以实现。