使用主题:
web.config中对所有页面应用主题
<!-- ********** 对所有页面应用某个主题 star ********** -->
<pages theme="Yellow" masterPageFile="~/Master/MasterPage.master" />
<!-- ********** 对所有页面应用某个主题 end ********** -->
对某个页面应用主题
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Master_Default" MasterPageFile="~/Master/MasterPage.master" Title="使用通用母板页的内容页面1" Theme="Yellow" %>
主题文件结构:
- App_Themes //主题文件夹
- Default //Default 主题文件夹
-Images //存放图片
-Controls.skin //定义控件皮肤样式
-Default.css //CSS
- Yellow
-Images
-Controls.skin
-Default.css
Controls.skin中定义:
<asp:TextBox runat="server" BorderStyle="dashed" BorderWidth="2px" />
ThemesSelector.ascx主题选择自定义控件
页面:
<b>选择主题:</b>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
</asp:DropDownList>
后台代码:
//Caching
using System.Web.Caching;
/// <summary>
/// 创建常用方法的静态类Helpers
/// </summary>
public static class Helpers
{
//获取App_Themes文件夹中定义的各主题路径数组
public static string[] GetThemes()
{
//缓存中存在
if (HttpContext.Current.Cache["SiteThemes"] != null)
{
//从缓存中获取数组数据
return (string[])HttpContext.Current.Cache["SiteThemes"];
}
//缓存中不存在,创建该Cache
else
{
//获取D:/.../App_Themes
string themesDirPath = HttpContext.Current.Server.MapPath("~/App_Themes");
//获取D:/.../App_Themes路径下包含D:/.../App_Themes/Default和D:/.../App_Themes/Yellow的数组
string[] themes = System.IO.Directory.GetDirectories(themesDirPath);
for(int i = 0; i <= themes.Length - 1; i++)
{
//获取路径文件名数组:Default,Yellow
themes[i] = System.IO.Path.GetFileName(themes[i]);
}
//监视路径跟改自动移除Cache
System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(themesDirPath);
//创建名为"SiteThemes",数据内容为Default,Yellow,监视路径为dep
HttpContext.Current.Cache.Insert("SiteThemes", themes, dep);
return themes;
}
}
}
//配置数据源,绑定数据
this.DropDownList1.DataSource = Helpers.GetThemes();
this.DropDownList1.DataBind();
//设置DropDownList的选中项
this.DropDownList1.SelectedValue = this.Page.Theme;
}
//存在id
if (id.Length > 0)
{
//如页面为回送并且表单中存在该id控件
//所有包含服务器端表单页面都有一个名为EVENTTARGET的隐藏字段,其中包含触发回送的控件ID
if (this.Request.Form["__EVENTTARGET"] == id && !string.IsNullOrEmpty(this.Request.Form[id]))
{
//重新设置当前页面主题
this.Theme = this.Request.Form[id];
//将当前选择主题保存在Session中
this.Session["CurrentTheme"] = this.Theme;
}
//如页面非回送,读取保存在Session["CurrentTheme"]中的主题信息
else
{
if(this.Session["CurrentTheme"] != null)
{
this.Theme = this.Session["CurrentTheme"].ToString();
}
}
}
base.OnPreInit(e);
}
}