改变网站的色彩
曾经是否想给您的网站的观众有机会去定制网站的色彩?这篇文章给出了使用ASP.NET来建立自己这样的网站的一个解决方案。在这里,我们使用ASP.NET母版页,所选择的语言是C#。
如msn.com和bbc.co.uk网站允许用户改变他们的网站模板颜色。这个想法看上去似乎是个噱头,并似乎有点毫无意义的,因为不是每个人都需要的,或者是符合每个人的口味。
但也有一些地方- 如MSN和BBC等大型组织会花很多钱在市场营销研究上。
首先,让我们看看演示的顺序和文件要求。我们使用Default.aspx开始,
所有的工作在 central.aspx 和master.cs中完成。
首先的一个关键点是样式是aspx文件而不是css文件。central.aspx文件渲染一个CSS文件,因为我们在文档内容的顶部声明。样式表如下:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#"%>
<%
string colormedium = null;
string colorstrong = null;
string colorweak = null;
string logo = null;
HttpCookie cookie = Request.Cookies["colorme"];
if (cookie == null) {
Random RandomClass = new Random();
int RandomNumber = 0;
RandomNumber = RandomClass.Next(1, 5);
if (RandomNumber == 1) {
colorstrong = "#55AAFB";// - strong blue
colormedium = "#8FC8FD";// - medium blue
colorweak = "#C9E5FF";// - weak blue
logo = "url('images/picassocode-blue.jpg')";//- blue logo
}
else if (RandomNumber == 2) {
colorstrong = "#AF51FF";// - strong purple
colormedium = "#CB8DFF";// - medium purple
colorweak = "#E6C9FF";// - weak purple
logo = "url('images/picassocode-purple.jpg')";//- purple logo
}
else if (RandomNumber == 3) {
colorstrong = "#FFBD4C";// - strong orange
colormedium = "#FFD189";// - medium orange
colorweak = "#FFE8C5";// - weak orange
logo = "url('images/picassocode-orange.jpg')";//- orange logo
}
else if (RandomNumber == 4) {
colorstrong = "#FF5489";// - strong pink
colormedium = "#FF91B8";// - medium pink
colorweak = "#FFCDE0";// - weak pink
logo = "url('images/picassocode-pink.jpg')";//- pink logo
}
else if (RandomNumber == 5) {
colorstrong = "#C3E473";// - strong green
colormedium = "#DDEFA5";// - medium green
colorweak = "#F1F9D7";// - weak green
logo = "url('images/picassocode-green.jpg')";//- green logo
}
}
else if (cookie != null) {
if (cookie.Value == "blue") {
colorstrong = "#55AAFB";// - strong blue
colormedium = "#8FC8FD";// - medium blue
colorweak = "#C9E5FF";// - weak blue
logo = "url('images/picassocode-blue.jpg')";//- blue logo
}
else if (cookie.Value == "purple") {
colorstrong = "#AF51FF";// - strong purple
colormedium = "#CB8DFF";// - medium purple
colorweak = "#E6C9FF";// - weak purple
logo = "url('images/picassocode-purple.jpg')";//- purple logo
}
else if (cookie.Value == "orange") {
colorstrong = "#FFBD4C";// - strong orange
colormedium = "#FFD189";// - medium orange
colorweak = "#FFE8C5";// - weak orange
logo = "url('images/picassocode-orange.jpg')";//- orange logo
}
else if (cookie.Value == "pink") {
colorstrong = "#FF5489";// - strong pink
colormedium = "#FF91B8";// - medium pink
colorweak = "#FFCDE0";// - weak pink
logo = "url('images/picassocode-pink.jpg')";//- pink logo
}
else if (cookie.Value == "green")
{
colorstrong = "#C3E473";// - strong green
colormedium = "#DDEFA5";// - medium green
colorweak = "#F1F9D7";// - weak green
logo = "url('images/picassocode-green.jpg')";//- green logo
}
}
%>
body { font-family: Tahoma,Verdana,
Arial;font-size:100%;padding:0;margin:0;
background-color:#CFCFB8;color:#9a9a9a}
a{ color:<% = colorstrong %>
text-decoration:none;
}
a:hover{
text-decoration:underline
}
table {
border-collapse : collapse;
}
table td, table th {
padding : 0;
}
.img_border{border:1px solid #000;margin:5px}
.logo{background-image:<% = logo%>}
.mediumback{background-color:<% = colormedium%>}
.mediumborder{border:solid 3px <% = colormedium%>}
.mediumtext{color:<% = colormedium%>}
.strongback{background-color:<% = colorstrong%>}
.strongborder{border:solid 3px <% = colorstrong%>}
.strongtext{color:<% = colorstrong%>}
.weakback{background-color:<% = colorweak%>}
.weakborder{border:solid 3px <% = colorweak%>}
.weaktext{color:<% = colorweak%>}
/* menu */
.mymenu{
margin: 5px 0;
padding: 0;
}
.mymenu a.menuitem{
background-color:<% =colorweak %>;
font-size:85%;
color: #000000;
display: block;
position: relative;
padding: 4px 0;
padding-left: 10px;
text-decoration: none;
border-bottom: 1px solid #9A9A9A;
text-align:left;
}
.mymenu a.menuitem:hover{
background-color:#ffffff;
}
当central.aspx加载时,服务器读取的C#的ASPX的内容,然后渲染为CSS到浏览器上。这是因为我们声明内容类型为""text/css"。这是因为我们声明我们的内容的类型为 "text/css"。
在central.aspx,我们判断是否有一个名字为"colorme"的cookie。如果没有,我们生成一个1至5之间的一个随机数和定义适当四个变量根据这个随机数。(这些变量将会用来设置logo , 弱色,中等色彩,和强烈的色彩)。 如果有一个名字为"'colorme
'"的cookie ,我们的四个变量设置到适当的logo和颜色。一旦四个变量的设置,不同的CSS样式被分配给相应的值。
以上发生,是在当页加载的时候-设置颜色
现在,提供用户他们能选择的颜色的权利,在我们的例子中有:green, orange, pink, blue, 和 purple
色箱子之下的超链接设置如下所示:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<tr>
<td></td>
<td style="text-align:right">Change colour</td>
<td><a href="default.aspx?colour=green"><img src="images/square-green.jpg"
alt="Change to green" class="img_border" style="vertical-align:middle"/></a></td>
<td><a href="default.aspx?colour=orange"><img src="images/square-orange.jpg"
alt="Change to orange" class="img_border" style="vertical-align:middle"/></a></td>
<td><a href="default.aspx?colour=pink"><img src="images/square-pink.jpg"
alt="Change to pink" class="img_border" style="vertical-align:middle"/></a></td>
<td><a href="default.aspx?colour=blue"><img src="images/square-blue.jpg"
alt="Change to blue" class="img_border" style="vertical-align:middle"/></a></td>
<td><a href="default.aspx?colour=purple"><img src="images/square-purple.jpg"
alt="Change purple" class="img_border" style="vertical-align:middle"/></a></td>
</tr>
</table>
当点击其中任何一个,Default.aspx的被调用,调用master.cs 和central.aspx。central.aspx将像上面说明的一样表现,master.cs留意URL中querystring的 'color' 。
master.cs 像下面所示:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
void Page_Load()
{
string getcolourcarry = Request.QueryString["colour"];
string getid = Request.QueryString["id"];
if (getcolourcarry != "")
{
changecolor(getcolourcarry);
}
if (getid == "control2")
{
mainblock.Controls.Clear();
Control c1 = LoadControl("control2.ascx");
mainblock.Controls.Add(c1);
}
else if (getid == "control3")
{
mainblock.Controls.Clear();
Control c1 = LoadControl("control3.ascx");
mainblock.Controls.Add(c1);
}
else
{
mainblock.Controls.Clear();
Control c1 = LoadControl("home.ascx");
mainblock.Controls.Add(c1);
}
}
void changecolor(string getcolour)
{
HttpCookie cookie = new HttpCookie("colorme");
if (getcolour == "green") {
cookie.Value = "green";
cookie.Expires = DateTime.Now.AddYears(1);
cookie.Domain = "picassocode.net";
Response.Cookies.Add(cookie);
}
else if (getcolour == "orange") {
cookie.Value = "orange";
cookie.Expires = DateTime.Now.AddYears(1);
cookie.Domain = "picassocode.net";
Response.Cookies.Add(cookie);
}
else if (getcolour == "pink") {
cookie.Value = "pink";
cookie.Expires = DateTime.Now.AddYears(1);
cookie.Domain = "picassocode.net";
Response.Cookies.Add(cookie);
}
else if (getcolour == "purple") {
cookie.Value = "purple";
cookie.Expires = DateTime.Now.AddYears(1);
cookie.Domain = "picassocode.net";
Response.Cookies.Add(cookie);
}
else if (getcolour == "blue")
{
cookie.Value = "blue";
cookie.Expires = DateTime.Now.AddYears(1);
cookie.Domain = "picassocode.net";
Response.Cookies.Add(cookie);
}
}
}
但页面加载的时候,我们要寻求两个东西:ID querystring 和colour querystring ,在我们的案例当中,ID只是指引我们到相应的页面,我们可以加载control2.ascx,control3.ascx,如果ID为空,home.ascx 将会被加载。
是我们寻求的colour querystring ,如果它不等于空,我们看到它是否等于我们的颜色之一-green, orange等,如果相等,我们根据颜色选择简单地创建一个Cookie。central.aspx被调用,通过查看是否有一个名字为 'colorme
' 的cookie ,并选择相关的样式。
注意,如果你设置了样式表这样做,您就可以让您的用户更改任何样式。
该方法是由Cookie驱动的。大多数用户有能力阻止和删除Cookie ,你可能需要添加一个帮助页面去描述。
查看这个的例子。
作者:朱祁林
出处:http://zhuqil.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利