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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
Forbes - Security
Forbes - Security
C
Cyber Attacks, Cyber Crime and Cyber Security
Latest news
Latest news
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
Vercel News
Vercel News
V
Vulnerabilities – Threatpost
I
InfoQ
GbyAI
GbyAI
有赞技术团队
有赞技术团队
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
F
Full Disclosure
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
Hugging Face - Blog
Hugging Face - Blog
Simon Willison's Weblog
Simon Willison's Weblog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
Engineering at Meta
Engineering at Meta
The Register - Security
The Register - Security
T
Tor Project blog
T
Troy Hunt's Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Security Affairs
W
WeLiveSecurity
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Stack Overflow Blog
Stack Overflow Blog
Apple Machine Learning Research
Apple Machine Learning Research
H
Heimdal Security Blog
S
Secure Thoughts
Y
Y Combinator Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Latest
Security Latest
Martin Fowler
Martin Fowler
G
Google Developers Blog
宝玉的分享
宝玉的分享
腾讯CDC
TaoSecurity Blog
TaoSecurity Blog
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Project Zero
Project Zero
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog

博客园 - 搞IT的狐狸

ASP.Net动态使用CSS - 搞IT的狐狸 - 博客园 SqlDataReader或DATATABLE DATASET 到EXCEL [转载]ROW_NUM实现分页 (转载)ASP.NET本地化 VS2005 TreeView 的 CheckBox 被点击时的引发页面回发事件 (转发) ASP.NET 2.0,C#----利用GridView控件导出其他文件(导出Excel,导出Word文件) 什么是SHTML 如何使用IFRAM JS来刷新UpdatePanel 随即验证码实现(根据韩现龙代码修改) [北京]招聘 网站设计师 我这半个月的代码总结(小技巧,小代码)之二 我这半个月的代码总结(小技巧,小代码)之一 将数据控件(如GridView)的内容转化成Excel格式文件 ASP.NET新手实用技巧!(C#) 学历不高!写给自己!自勉! 一张笑死我又让我辛酸的图片!开发人员的辛酸! 服务器刚开,凑个沙发!.NET几点你应该知道的细节! 求动态绑定控件ID的值的方法!着急!
用Themes实现网站换肤
搞IT的狐狸 · 2007-08-21 · via 博客园 - 搞IT的狐狸

一、简介:

  利用Themes我们可以很容易的更改控件、页面的风格,而不需要修改我们的代码和页面文件。Themes文件被单独的放在1个App_Themes文件夹下面,与你的程序是完全分开的。

  二、怎么使用Themes和Skins:  先看个非常简单的实例:

App_Themes\default\1.skin文件代码:

<asp:Label Font-Bold="true" ForeColor="Red" runat="server" />

  default.aspx:文件代码:

<%@ Page Language="C#" Theme="default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Page with Example Theme Applied</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Hello 1" /><br />
<asp:Label ID="Label2" runat="server" Text="Hello 2" /><br />
</form>
</body>
</html>

  可以看到我们在default.aspx并没有写如何的控制style的代码,但运行取发现label上的字都变成了粗体红色了,这就是1个最基本的theme例子。

  App_Themes文件夹:

  App_Themes文件夹位于程序的根目录下,App_Themes下必须是Theme名称的子文件夹,子文件夹中可以包含多个.skin和.css文件。建立2个Theme,名称分别为default和default2:

  使用themes  1、在1个页面中应用Theme:

  如果想在某1个页面中应用Theme,直接在aspx文件中修改<%@ Page Theme="..." %>,比如你想这个页面应用default2 theme,设置<%@ Page Theme="default2" %>就OK

  2、在所有页面应用同1个Theme:

  如果要在所有页面上使用相同的Theme,在web.config中的<system.web>节点下加上句<pages theme="..."/>

  3、让控件不应用Theme:

  第1个例子中我们看到了2个Label的风格都变了,就是说.skin文件中的风格在页面上所有Label都起作用了。但有时我们希望某1个Label不应用.skin中的风格,这时你只需设置Label的EnableTheming属性为false的时候就可以了。

  也许你还想不同的label显示不同的风格,你只需设置label的SkinID属性就可以,见下面的实例:

App_Themes\default\1.skin

<asp:label runat="server" font-bold="true" forecolor="Red" />
<asp:label runat="server" SkinID="Blue" font-bold="true" forecolor="blue" />

deafult.aspx

<%@ Page Language="C#" Theme="default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Page with Example Theme Applied</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label2" runat="server" Text="Hello 2" SkinID="Blue" /><br />
<asp:Label ID="Label3" runat="server" Text="Hello 3" /><br />
</form>
</body>
</html>


  运行后就会发现2个label显示的风格不一样了。

  4、其他方法:

  前面已经说了在aspx文件头使用 <%@ Page Theme="..." %> 来使用theme,而用这个方法应用theme中的风格将会覆盖你写在aspx中的控件属性style。比如:

  App_Themes\default\1.skin

<asp:Label Font-Bold="true" ForeColor="Red" runat="server" />

  default.aspx

<%@ Page Language="C#" Theme="default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Hello 1" /><br />
<asp:Label ID="Label2" runat="server" Text="Hello 2" ForeColor="blue" />
</form>
</body>
</html>


  运行结果,所有的label的forecolor都为red。

  而使用<%@ Page StyleSheetTheme="..." %>应用theme就不会覆盖你在aspx文件中写的属性style:

  控件应用style属性的顺序如下:

  a、StyleSheetTheme引用的风格

  b、代码设定的控件属性(覆盖StyleSheetTheme)

  c、Theme引用的风格(覆盖前面2个)

  theme中包含CSS:

  theme中也可以使用.css文件,当你把.css文件放在1个theme目录下后,在用到了这个theme的页面中自动会应用你的.css的

  三、后台代码轻松为网站换府肤  前面讲的都是在aspx文件或web.config中应用theme,而在blog这样的每个用户都有不同的skin的网站中用上面的方法来实现换skin显然是不方便的。

  下面就介绍怎么在后台代码中动态的引用theme来解决上面的情况,theme必须在page被请求的最早期就应用上,所以我们必须在Page_PreInit事件中写代码,代码很简单,就1句:

Page.Theme = "...";

  这里我们只要从数据库中去读取每个用户设置的不同theme名就可以轻松实现每个用户都有不同的skin了。