TheBeerHouse---加载主题

学习theBeerhouse的原代码,今天了解下动态加载主题.

从母板页中查看,主题切换由自定义控件ThemeSelector.ascx实现.

None.gif <% @ Control Language = " C# "  AutoEventWireup = " true "  CodeFile = " ThemeSelector.ascx.cs "  Inherits = " MB.TheBeerHouse.UI.Controls.ThemeSelector "   %>
None.gif
< b >< asp:Localize runat = " server "  ID = " locTheme "  meta:resourcekey = " locThemeResource1 "  Text = " Theme:  " ></ asp:Localize ></ b >
None.gif
< asp:DropDownList runat = " server "  ID = " ddlThemes "  AutoPostBack = " True "  meta:resourcekey = " ddlThemesResource1 "   />

 这里就来了一个我以前没注意的控件了(看MSND)..  初步看来和Label没什么区别!
resourcekey获取或设置资源的名称,该名称可用作查找资源值的键, 这里是为了实现网站的本地化,所以我就不再深究了.

所以这里我完全可以用Label改写了.
现在我们来看它的cs代码:
None.gif        protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif         
if (Globals.ThemesSelectorID.Length == 0)
InBlock.gif            Globals.ThemesSelectorID 
= ddlThemes.UniqueID;
InBlock.gif
InBlock.gif         ddlThemes.DataSource 
= Helpers.GetThemes();
InBlock.gif         ddlThemes.DataBind();
InBlock.gif
InBlock.gif         ddlThemes.SelectedValue 
= this.Page.Theme;
ExpandedBlockEnd.gif      }
这里,他在Globals.cs这个类中定义了一个静态变量:
None.gif     public   static   class  Globals
ExpandedBlockStart.gifContractedBlock.gif   
dot.gif {
InBlock.gif      
public static string ThemesSelectorID = "";
InBlock.gif
InBlock.gif      
static Globals()
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif       
ExpandedSubBlockEnd.gif      }
  
ExpandedBlockEnd.gif}

这个静态变量用来取得ddlThemes的ID,再下一句是设置ddlThemes的数据源,看Helpers这个类
None.gif        public   static   string [] GetThemes()
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif         
if (HttpContext.Current.Cache["SiteThemes"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            
return (string[])HttpContext.Current.Cache["SiteThemes"];
ExpandedSubBlockEnd.gif         }

InBlock.gif         
else
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            
string themesDirPath = HttpContext.Current.Server.MapPath("~/App_Themes");
InBlock.gif            
// get the array of themes folders under /app_themes
InBlock.gif
            string[] themes = Directory.GetDirectories(themesDirPath);
InBlock.gif            
for (int i = 0; i <= themes.Length - 1; i++)
InBlock.gif                themes[i] 
= Path.GetFileName(themes[i]);
InBlock.gif            
// cache the array with a dependency to the folder
InBlock.gif
            CacheDependency dep = new CacheDependency(themesDirPath);
InBlock.gif            HttpContext.Current.Cache.Insert(
"SiteThemes", themes, dep);
InBlock.gif            
return themes;
ExpandedSubBlockEnd.gif         }

ExpandedBlockEnd.gif      }

这里的功能就是搜索你的主题文件夹中有几个主题,下次你添了主题,其他的你什么都不要做了,主题自然会出现在页面上.

ddlThemes.SelectedValue = this.Page.Theme;这句就是指定页面的主题为选择的主题了.

这里有一定值得注意的地方:
Theme 属性只能在 PreInit 事件之前进行设置,在 PreInit 事件之后设置 Theme 属性将引发 InvalidOperationException 异常。

指定的主题必须作为应用程序主题或全局主题存在。如果该主题不存在,则将引发 HttpException 异常。

为了让主题能够在PreInit事件中加载.我们再设计一个类Basepage:

None.gif      public   class  BasePage : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
protected override void OnPreInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string id = Globals.TemeSelectId;
InBlock.gif            
if (id.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// if this is a postback caused by the theme selector's dropdownlist,
InBlock.gif                
// retrieve the selected theme and use it for the current page request
InBlock.gif
                if (this.Request.Form["__EVENTTARGET"== id &&
InBlock.gif                   
!string.IsNullOrEmpty(this.Request.Form[id]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.Theme = this.Request.Form[id];
InBlock.gif                    
this.Session["CurrentTheme"= this.Theme;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
// if not a postback, or a postback caused by controls other then
InBlock.gif                    
// the theme selector, set the page's theme with the value found
InBlock.gif                    
// in Session, if present
InBlock.gif
                    if (this.Session["CurrentTheme"!= null)
InBlock.gif                        
this.Theme = this.Session["CurrentTheme"].ToString();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
base.OnPreInit(e);
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }


好了.现在让页面的cs代码这样就好了.
None.gif public  partial  class  _Default: BasePage  // 继承自BasePage;
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
点击下载源文件 可以发现,现在站点的样式是统一的!
2:00 了.该睡了..明天还要上课呢..呵呵..

转载于:https://www.cnblogs.com/sliuqin/archive/2007/05/24/757873.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值