预初始化对象(OnPreInit)
原文链接:https://www.cnblogs.com/mimengjiangnan/archive/2007/11/03/947912.html
实现功能:动态控制网站程序中服务器控件的外观。
实现步骤:
一在网站根目录下创建文件夹:App_Themes。这个文件夹名字不能改成别的。
二在App_Themes文件夹下创建子文件夹(主题),这个子文件夹名称就是页面的主题名称
三在主题下添加.css,.skin等文件.
四示例:
1 创建文件夹: App_Themes,在它下面添加两个主题: BlueTheme和RedTheme.
2 在BlueTheme下添加文件BlueTheme.skin,写入内容:
<asp:Button runat="server" BackColor="Blue" ForeColor="White" Font-Name="Arial" Font-Size="9px" />
3 在RedTheme下添加文件RedTheme.skin,写入内容:
<asp:Button runat="server" BackColor="Red" ForeColor="White" Font-Name="Arial" Font-Size="9px" />
4 HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageLife.aspx.cs" Inherits="PageLife" %>
<!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 runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="PageLife.aspx?theme=Red" >red</a>
<a href="PageLife.aspx?theme=Blue" >blue</a>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Button ID="Button2" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
5 .CS代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class PageLife : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
///<summary>
///预初始化方法,在用户控件和模板页里没有这个事件
///</summary>
///<param name="e"></param>
protected override void OnPreInit(EventArgs e)
{
//参考http://msdn2.microsoft.com/zh-cn/library/tx35bd89(vs.80).aspx
base.OnPreInit(e);
switch (Request.QueryString["theme"])
{
case "Blue":
Page.Theme = "BlueTheme";
break;
case "Red":
Page.Theme = "RedTheme";
break;
}
}
}