Asp.Net2.0实现自定义样式
我们在做一个项目时有时候需要动态改变所有页面的样式,但当我们在Page指令或者Web.Config里设置Theme时有一个问题,我无法通过程序动态且一致改变所有页面的样式,推荐一个方法,就是改写OnPreInit事件,在App_Code里新建一个PageBase类,他继承自System.Web.UI.Page,然后所有页面再继承自PageBase,就搞定了,具体实现就自己看吧:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// PageBase 的摘要说明
/// </summary>
public class PageBase:System.Web.UI.Page
{
public PageBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Request.Cookies["skin"] != null && Request.QueryString["skin"] == null)
{
Page.Theme = Request.Cookies["skin"].Value.ToString();
}
else
{
if (Request.QueryString["skin"] != null)
{
HttpCookie c = new HttpCookie("skin", Request.QueryString["skin"]);
Response.Cookies.Add(c);
Page.Theme = Request.Cookies["skin"].Value.ToString();
}
else
Page.Theme = "skin";
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// PageBase 的摘要说明
/// </summary>
public class PageBase:System.Web.UI.Page
{
public PageBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (Request.Cookies["skin"] != null && Request.QueryString["skin"] == null)
{
Page.Theme = Request.Cookies["skin"].Value.ToString();
}
else
{
if (Request.QueryString["skin"] != null)
{
HttpCookie c = new HttpCookie("skin", Request.QueryString["skin"]);
Response.Cookies.Add(c);
Page.Theme = Request.Cookies["skin"].Value.ToString();
}
else
Page.Theme = "skin";
}
}
}