?? skinnedcommunitycontrol.cs
字號:
namespace ASPNET.StarterKit.Communities {
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Web.Security;
using System.Web.Caching;
//*********************************************************************
//
// SkinnedCommunityControl Class
//
// This is the base class of all controls in the ASP.NET Community
// Starter Kit. This class enables pages, content, and controls to
// load different skins.
//
//*********************************************************************
[ParseChildren(true)]
public abstract class SkinnedCommunityControl : WebControl, INamingContainer {
protected UserInfo objUserInfo;
protected PageInfo objPageInfo;
protected SectionInfo objSectionInfo;
protected CommunityInfo objCommunityInfo;
string _skinName = null;
string _skinFileName = null;
//*********************************************************************
//
// SkinnedCommunityControl Constructor
//
// Retrieves the UserInfo, SectionInfo, and PageInfo from Context
// making these objects automatically available to all controls
// in the framework.
//
//*********************************************************************
public SkinnedCommunityControl() {
if (Context != null) {
objUserInfo = (UserInfo)HttpContext.Current.Items[ "UserInfo" ];
objPageInfo = (PageInfo)HttpContext.Current.Items[ "PageInfo" ];
objSectionInfo = (SectionInfo)HttpContext.Current.Items[ "SectionInfo" ];
objCommunityInfo = (CommunityInfo)HttpContext.Current.Items[ "CommunityInfo" ];
}
}
//*********************************************************************
//
// Controls Property
//
// If you attempt to access a child control, make sure that the
// CreateChildControls method has been called.
//
//*********************************************************************
override public ControlCollection Controls {
get {
EnsureChildControls();
return base.Controls;
}
}
//*********************************************************************
//
// CreateChildControls Method
//
// Loads the skin for this control, calls the InitializeSkin
// method of the derived control, and adds the skin to the controls
// collection.
//
//*********************************************************************
protected override void CreateChildControls() {
Control skin;
// Load the skin
skin = LoadSkin();
Controls.Add(skin);
// Initialize the skin
InitializeSkin(skin);
}
private string BuildSkinKey() {
// Do we have a skin file?
if (_skinFileName == null)
throw new Exception("The SkinFileName property of the control must be set.");
// Do we have a skin name?
if (_skinName == null)
_skinName = objSectionInfo.Skin;
return ("/Communities/" + _skinName + "/Skins/" + SkinType + "/" + _skinFileName).ToLower();
}
private string BuildFailOverSkinKey()
{
// Do we have a skin file?
if (_skinFileName == null)
throw new Exception("The SkinFileName property of the control must be set.");
_skinName = objSectionInfo.FailOverSkin;
return ("/Communities/" + _skinName + "/Skins/" + SkinType + "/" + _skinFileName).ToLower();
}
private string BuildDefaultSkinKey() {
// Do we have a skin file?
if (_skinFileName == null)
throw new Exception("The SkinFileName property of the control must be set.");
return ("/Communities/Common/Themes/Default/Skins/" + SkinType + "/" + _skinFileName).ToLower();
}
//*********************************************************************
//
// LoadSkin Method
//
// Loads the skin for this control. If the skin cannot be loaded,
// fails over to loading the default skin and marks this fact
// in the Cache.
//
//*********************************************************************
protected Control LoadSkin() {
Control skin;
// Build the skin key
string skinKey = BuildSkinKey();
// SMR - Enh - Begin: Add fail over skin support
// If skin doesn't exist, failover to FailOverSkin
if (Context.Cache[skinKey] != null)
{
skinKey = BuildFailOverSkinKey();
Context.Trace.Warn( "Failover: " + skinKey );
}
// SMR - Enh - End: Add fail over skin support
// If skin doesn't exist, failover to Default
if (Context.Cache[skinKey] != null) {
skinKey = BuildDefaultSkinKey();
Context.Trace.Warn( "Failover: " + skinKey );
}
// Attempt to load the control.
try {
skin = Page.LoadControl( CommunityGlobals.AppPath + skinKey);
}
catch (FileNotFoundException fnfEx)
{
Context.Trace.Warn( "community error", "hard fail file not found: " + skinKey, fnfEx );
// Add a marker we can check for to skip this in the future
Context.Cache.Insert(skinKey, "fnf",
new CacheDependency(Page.MapPath(CommunityGlobals.AppPath + skinKey)));
// SMR - Enh - Begin: Add fail over skin support
try
{
skinKey = BuildFailOverSkinKey();
skin = Page.LoadControl( CommunityGlobals.AppPath + skinKey);
}
catch
{
// Load Default Skin
skinKey = BuildDefaultSkinKey();
try
{
skin = Page.LoadControl( CommunityGlobals.AppPath + skinKey);
}
catch
{
throw new Exception( "Could not failover to " + CommunityGlobals.AppPath + skinKey );
}
}
// SMR - Enh - End: Add fail over skin support
}
return skin;
}
private string BuildTemplateSkinKey(string skinFileName) {
return ("/Communities/" + _skinName + "/Skins/TemplateSkins/" + skinFileName).ToLower();
}
private string BuildTemplateDefaultSkinKey(string skinFileName) {
// Do we have a skin name?
if (_skinName == null)
_skinName = objSectionInfo.Skin;
return ("/Communities/Common/Themes/Default/Skins/TemplateSkins/" + skinFileName).ToLower();
}
//*********************************************************************
//
// LoadTemplate Method
//
// Loads a Template skin. If the skin cannot be loaded, fails
// over to the default skin and marks this fact in the Cache.
//
//*********************************************************************
protected ITemplate LoadTemplate(string skinFileName) {
ITemplate skin;
string skinKey = BuildTemplateSkinKey(skinFileName);
// If skin doesn't exist, failover to Default
if (Context.Cache[skinKey] != null) {
skinKey = BuildTemplateDefaultSkinKey(skinFileName);
Context.Trace.Warn( "template Failover: " + skinKey );
}
// Attempt to load the template.
try {
skin = Page.LoadTemplate( CommunityGlobals.AppPath + skinKey);
} catch (FileNotFoundException fnfEx) {
Context.Trace.Warn( "error", "template hard fail: " + skinKey, fnfEx );
// Add a marker we can check for to skip this in the future
Context.Cache.Insert(skinKey, "fnf",
new CacheDependency(Page.MapPath(CommunityGlobals.AppPath + skinKey)));
// Load Default Skin
skinKey = BuildTemplateDefaultSkinKey(skinFileName);
skin = Page.LoadTemplate( CommunityGlobals.AppPath + skinKey);
}
return skin;
}
//*********************************************************************
//
// GetControl Method
//
// Retrieves a control from a skin. If the control cannot be
// retrieved, throws an exception.
//
//*********************************************************************
protected Control GetControl(Control skin, string controlID) {
Control ctlFoundControl = skin.FindControl( controlID );
if (ctlFoundControl == null)
throw new Exception( "Could not find " + controlID + " in skin!" );
return ctlFoundControl;
}
//*********************************************************************
//
// GetOptionalControl Method
//
// Retrieves a control from a skin. If the control cannot be
// retrieved, DOES NOT throw an exception.
//
//*********************************************************************
protected Control GetOptionalControl(Control skin, string controlID) {
Control ctlFoundControl = skin.FindControl( controlID );
return ctlFoundControl;
}
//*********************************************************************
//
// InitializeSkin Method
//
// This method must be implemented by all derived controls. The
// inherited method retrieves all the controls that it needs from
// the skin.
//
//*********************************************************************
protected abstract void InitializeSkin(Control skin);
//*********************************************************************
//
// SkinType Property
//
// The type of skin. For example Page, Control, Template
//
//*********************************************************************
protected abstract string SkinType {
get;
}
//*********************************************************************
//
// SkinName Property
//
// The name of the skin used with this control.
// For example Default, Arc, Robotico.
//
//*********************************************************************
public string SkinName {
get {
return _skinName;
}
set {
_skinName = value;
}
}
//*********************************************************************
//
// SkinFileName Property
//
// The file name of the skin.
// For example, Sections_Section.ascx, Sections_SectionBottom.ascx
//
//*********************************************************************
public string SkinFileName {
get {
return _skinFileName;
}
set {
_skinFileName = value;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -