动态加载主题文件(2种方法)

动态加载主题应该有很多种方法的,我想到有2种,
不过在写代码的时候有一点是需要注意的:必须在Page_PreInit事件处理程序中修改Theme属性值。
下面分别给出2种代码,第二种是通过Session来解决这个问题,还得设置Global.asax,稍显得麻烦!
一个Web应用程序有很多页面,如果用第一种方法,每次都得写这些该死得代码,确实挺烦人的,用Seesion的好处就不言而喻了。
方法一:
ContractedBlock.gif ExpandedBlockStart.gif Default.aspx
None.gif<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
None.gif
None.gif
<script runat="server">
None.gif    
void Page_PreInit(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
//设置页面所设置的主题
InBlock.gif
        string theme="";
InBlock.gif        
if (Request.QueryString["theme"== null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            theme 
= "Red";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            theme 
= Request.QueryString["theme"];
ExpandedSubBlockEnd.gif        }

InBlock.gif        Page.Theme 
= theme;
InBlock.gif        
//设置DropDownList控件的选中项
InBlock.gif
        ListItem item = DropDownList1.Items.FindByValue(theme);
InBlock.gif        
if (item != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            item.Selected 
= true;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif    
void SelectedIndexChanged(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
//获取DropDownList选中项值,并进行页面重定向
InBlock.gif
        string url = Request.Path + "?theme=" + DropDownList1.SelectedItem.Value;
InBlock.gif        Response.Redirect(url);
ExpandedBlockEnd.gif    }

None.gif
</script>
None.gif
None.gif
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
None.gif
<html xmlns="http://www.w3.org/1999/xhtml">
None.gif
<head runat="server">
None.gif    
<title>示例6-5</title>
None.gif
</head>
None.gif
<body>
None.gif    
<form id="form1" runat="server">
None.gif        
<div>
None.gif            
<fieldset style="width: 210px">
None.gif                
<legend>动态加载主题</legend>
None.gif                
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="SelectedIndexChanged"
None.gif                    AutoPostBack
="True">
None.gif                    
<asp:ListItem Value="Red">启用Red主题</asp:ListItem>
None.gif                    
<asp:ListItem Value="Blue">启用Blue主题</asp:ListItem>
None.gif                
</asp:DropDownList>
None.gif                
<br />
None.gif                
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
None.gif            
</fieldset>
None.gif        
</div>
None.gif    
</form>
None.gif
</body>
None.gif
</html>


方法二:稍微复杂点
在Global.asax中设置如下:
void Session_Start(object sender, EventArgs e)
    {
        // 在新会话启动时运行的代码
        Session["CurrentTheme"] = "Red";
    }

ContractedBlock.gif ExpandedBlockStart.gif Default.aspx.cs
None.gifpublic partial class _Default : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected void Page_PreInit(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//设置页面所设置的主题
InBlock.gif
        if (Session["CurrentTheme"].ToString() == String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Session[
"CurrentTheme"= "Red";
ExpandedSubBlockEnd.gif        }

InBlock.gif        Page.Theme 
= Session["CurrentTheme"].ToString();
InBlock.gif
InBlock.gif        
//设置DropDownList控件的选中项
InBlock.gif
        ListItem item = DropDownList1.Items.FindByValue(Page.Theme);
InBlock.gif        
if (item != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            item.Selected 
= true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void SelectedIndexChanged(Object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//获取DropDownList选中项值,并进行页面重定向        
InBlock.gif
        Response.Redirect("~/SetTheme.aspx?Theme=" + DropDownList1.SelectedItem.Value);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ContractedBlock.gif ExpandedBlockStart.gif SetTheme.aspx.cs
None.gifpublic partial class SetTheme : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Session[
"CurrentTheme"= Request.Params["Theme"];
InBlock.gif        Response.Redirect(
"~/Default.aspx");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

终于写完了,收工了~第一个Demo是ASP.NET2.0开发指南上的,第二个是在这个基础上稍微改了一下。

转载于:https://www.cnblogs.com/hide0511/archive/2006/09/22/512277.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值