动态加载主题应该有很多种方法的,我想到有2种,
不过在写代码的时候有一点是需要注意的:必须在Page_PreInit事件处理程序中修改Theme属性值。
下面分别给出2种代码,第二种是通过Session来解决这个问题,还得设置Global.asax,稍显得麻烦!
一个Web应用程序有很多页面,如果用第一种方法,每次都得写这些该死得代码,确实挺烦人的,用Seesion的好处就不言而喻了。
方法一:


<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
< script runat ="server" >
void Page_PreInit(Object sender, EventArgs e)
{
// 设置页面所设置的主题
string theme = "" ;
if (Request.QueryString[ " theme " ] == null )
{
theme = " Red " ;
}
else
{
theme = Request.QueryString[ " theme " ];
}
Page.Theme = theme;
// 设置DropDownList控件的选中项
ListItem item = DropDownList1.Items.FindByValue(theme);
if (item != null )
{
item.Selected = true ;
}
}
void SelectedIndexChanged(Object sender, EventArgs e)
{
// 获取DropDownList选中项值,并进行页面重定向
string url = Request.Path + " ?theme= " + DropDownList1.SelectedItem.Value;
Response.Redirect(url);
}
</ script >
<! 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 > 示例6-5 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< fieldset style ="width: 210px" >
< legend > 动态加载主题 </ legend >
< asp:DropDownList ID ="DropDownList1" runat ="server" OnSelectedIndexChanged ="SelectedIndexChanged"
AutoPostBack ="True" >
< asp:ListItem Value ="Red" > 启用Red主题 </ asp:ListItem >
< asp:ListItem Value ="Blue" > 启用Blue主题 </ asp:ListItem >
</ asp:DropDownList >
< br />
< asp:Calendar ID ="Calendar1" runat ="server" ></ asp:Calendar >
</ fieldset >
</ div >
</ form >
</ body >
</ html >
< script runat ="server" >
void Page_PreInit(Object sender, EventArgs e)
{
// 设置页面所设置的主题
string theme = "" ;
if (Request.QueryString[ " theme " ] == null )
{
theme = " Red " ;
}
else
{
theme = Request.QueryString[ " theme " ];
}
Page.Theme = theme;
// 设置DropDownList控件的选中项
ListItem item = DropDownList1.Items.FindByValue(theme);
if (item != null )
{
item.Selected = true ;
}
}
void SelectedIndexChanged(Object sender, EventArgs e)
{
// 获取DropDownList选中项值,并进行页面重定向
string url = Request.Path + " ?theme= " + DropDownList1.SelectedItem.Value;
Response.Redirect(url);
}
</ script >
<! 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 > 示例6-5 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< fieldset style ="width: 210px" >
< legend > 动态加载主题 </ legend >
< asp:DropDownList ID ="DropDownList1" runat ="server" OnSelectedIndexChanged ="SelectedIndexChanged"
AutoPostBack ="True" >
< asp:ListItem Value ="Red" > 启用Red主题 </ asp:ListItem >
< asp:ListItem Value ="Blue" > 启用Blue主题 </ asp:ListItem >
</ asp:DropDownList >
< br />
< asp:Calendar ID ="Calendar1" runat ="server" ></ asp:Calendar >
</ fieldset >
</ div >
</ form >
</ body >
</ html >
方法二:稍微复杂点
在Global.asax中设置如下:
void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
Session["CurrentTheme"] = "Red";
}


public
partial
class
_Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
}
protected void Page_PreInit( object sender, EventArgs e)
{
// 设置页面所设置的主题
if (Session[ " CurrentTheme " ].ToString() == String.Empty)
{
Session[ " CurrentTheme " ] = " Red " ;
}
Page.Theme = Session[ " CurrentTheme " ].ToString();
// 设置DropDownList控件的选中项
ListItem item = DropDownList1.Items.FindByValue(Page.Theme);
if (item != null )
{
item.Selected = true ;
}
}
public void SelectedIndexChanged(Object sender, EventArgs e)
{
// 获取DropDownList选中项值,并进行页面重定向
Response.Redirect( " ~/SetTheme.aspx?Theme= " + DropDownList1.SelectedItem.Value);
}
}
{
protected void Page_Load( object sender, EventArgs e)
{
}
protected void Page_PreInit( object sender, EventArgs e)
{
// 设置页面所设置的主题
if (Session[ " CurrentTheme " ].ToString() == String.Empty)
{
Session[ " CurrentTheme " ] = " Red " ;
}
Page.Theme = Session[ " CurrentTheme " ].ToString();
// 设置DropDownList控件的选中项
ListItem item = DropDownList1.Items.FindByValue(Page.Theme);
if (item != null )
{
item.Selected = true ;
}
}
public void SelectedIndexChanged(Object sender, EventArgs e)
{
// 获取DropDownList选中项值,并进行页面重定向
Response.Redirect( " ~/SetTheme.aspx?Theme= " + DropDownList1.SelectedItem.Value);
}
}


public
partial
class
SetTheme : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
Session[ " CurrentTheme " ] = Request.Params[ " Theme " ];
Response.Redirect( " ~/Default.aspx " );
}
}
{
protected void Page_Load( object sender, EventArgs e)
{
Session[ " CurrentTheme " ] = Request.Params[ " Theme " ];
Response.Redirect( " ~/Default.aspx " );
}
}