惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
P
Privacy International News Feed
Security Latest
Security Latest
H
Hacker News: Front Page
T
Tenable Blog
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
Project Zero
Project Zero
O
OpenAI News
AI
AI
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
NISL@THU
NISL@THU
A
Arctic Wolf
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
U
Unit 42
S
Security Affairs
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 【当耐特】
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
月光博客
月光博客
Engineering at Meta
Engineering at Meta
腾讯CDC
F
Full Disclosure
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
The Cloudflare Blog

博客园 - sooloo

asp.net遍历控件的实现 [转]Creating Custom Web Controls in C# Stats(演示了如何创建一个导航条) [转]创建动态数据输入用户界面(ASP.NET 中的动态控件入门) [转]ASP.NET 页面对象模型 [转]基于功能更丰富的基础类构建您自己的 ASP.NET 页面 [转]ASP.NET中促进代码重用的2种模式 [转] 动态加载Asp.net分页控件 [转] more than one way to skin an app [转]模拟Asp.Net Forums实现可以换皮肤的控件 [转]在ASP.Net中两种利用CSS实现多界面的方法 DotText分析---新Blog注册 转:dotext数据库篇 Vs2003调试DotText时碰到的一个问题 DotText数据库分析(2) CnDotText数据库分析(1) 高程历年试题及答案 [转帖]追MM与设计模式 学习cnblogsGuestBook V2.0 --(2) 学习cnblogsGuestBook V2.0
[原创]利用CSS实现页面换肤
sooloo · 2005-09-22 · via 博客园 - sooloo

     常常看到有的网站通过DropDownList、菜单或按钮实现对整个网站改变页面风格(换肤)。专门Google了一些这方面的资料,总结了一下实现以上功能大致可以分成两类:
1、整个网站统一使用一个CSS文件,保证一类风格。通过切换不同风格的CSS文件实现换肤。这种方法实现
      起来比较简单。
2、不同风格、不同布局的UseControl,使用相同的后台代码(业务逻辑、功能)。通过重定向页面实现
     页面换肤。这种方法比较复杂,以后再讨论。

先看第一种方法:切换CSS文件换肤。
1、正常的页面如果需要指定所引用的CSS文件,必须在<head></head>里用<link>控件指定CSS文件,如

<head>

 
<link  rel=stylesheet  type=text/css  href=mycss.css />
</head>

     如果需要切换CSS,就不能再定死这个<link>了。可以通过后台代码动态地改变这个控件的属性。
     可以用<asp:placeholder>控件来代替。placeholder控件可以理解成一个控件的容器,就像一个透明的包,你往里面装什么,它就是什么。
     现在页面变成了这个样子,我们在里面放了一个空的placeholder来代替link.

<head>
    
    
<asp:placeholder id="MyCss" runat="server"/>
</head>

    在后台代码中定义一个函数,负责往placeholder里装东西,这里装css文件

public void ChangeSkin(string cssFile)
{
    
//生成一个新的HtmlGenericControl控件,它是一个link控件
    HtmlGenericControl objLink = new HtmlGenericControl("link");
    
//定义这个Link的各项属性
    objLink.ID = ID;
    objLink.Attributes[
"rel"= "stylesheet";
    objLink.Attributes[
"type"= "text/css";
    objLink.Attributes[
"href"= cssFile;
    
//把Link控件加到PlaceHolder控件中去
    MyCss.Controls.Add(objLink);
}

到这里,可以在页面里放2个按钮,按按钮切换不同的CSS文件

2、有的网站能把用户选择的样式保存起来,下次访问继续保留上次选择的风格。
     对于此类,可以用客户端的Cookie来保留选择的样式。

private void Page_Load(object sender, System.EventArgs e)
{
          
// 在此处放置用户代码以初始化页面
          if(!Page.IsPostBack)
         {

         
//判断客户端的浏览器是否支持Cookie
          if(Request.Browser.Cookies == false)   
                        label1.Text 
= "浏览器不支持Cookie !";
          
else
                        label1.Text = "浏览器支持Cookie !";
     
//客户端是否有Skin信息保存
         if(Request.Cookies["Skin"== null)
       
{
                  ChangeSkin(
"default.css");
                  label2.Text 
= "Cookie 为空,默认CSS";
        }

        
else
      
{
        
//根据客户端保留的Cookie信息,来加载不同的CSS样式表文件
         switch(Request.Cookies["Skin"].Value)
       
{
                
case "Red" : ChangeSkin("mycss2.css");
                                        label2.Text 
= "Cookie中的CSS为红色";
                                      
break;
                  
case "Blue" : ChangeSkin("mycss.css");
                                       label2.Text 
= "Cookie中的CSS为蓝色";
                                       
break;  
                       
default:     ChangeSkin("default.css");
//            break;
        }

      }

               

}

     
    当用户按下按钮选择样式的时候,还需要保留这些信息到Cookie中。

        public void SetCookie(string style)
        {

                HttpCookie myCookie 

= new HttpCookie("Skin");
                myCookie.Value 
= style;
                Response.Cookies.Add(myCookie);

        }

        public void btClick(object src, EventArgs e)
        {
            SetCookie(
"Red");

            ChangeSkin(

"mycss2.css");
            label2.Text 
= "现在设为红色" ;
            
        }