自定义控件:广告内容后期加载。以及NamingContainer层次的应用
《轉》http://www.pin5i.com/showtopic-11037.html
網站上的廣告內容可能會因加載過慢而導致整個網頁加載過慢,我們可以考慮將廣告內全部放在網頁最底部,等整個頁面加載完畢後再采用javascript使其顯示,考慮給控件一個TargetContainerID標識,控件廣告內容將要被顯示的容器ID,然后從控件自己所處的 開始向上查找該ID所指定的控件,(我們只向上找而沒有向下找,并且沒有處理某層次的子NamingContainer,所以不一定能夠找到,這里沒有考慮從Page對象向下遞歸查找主要考慮為了提高性能),如果沒有找到,則考慮給用戶一個事件讓用戶自己處理TargetContainer。
下面的自定義控件對此實現了封裝:
namespace HBZ.Ads.Controls
{
? ? using System;
? ? using System.ComponentModel;
? ? using System.Web;
? ? using System.Web.UI;
? ? using System.Web.UI.WebControls;
? ? using HBZ.Ads;
? ? using System.Collections.Generic;
? ? using System.Text;
? ? [Designer( typeof( HBZ.Ads.Controls.AdRotatorDesigner ) )]
? ? public class AdRotator : WebControl
? ? {
? ? ? ? private Dictionary<string , Control> findControlHelperCacheList = new Dictionary<string , Control>( );
? ? ? ? private readonly string scriptFormat = "var {0}_target = document.getElementById(\"{0}\"); var {1}_base = document.getElementById(\"{1}\"); if ({0}_target) {{ {0}_target.innerHTML = {1}_base.innerHTML; {1}_base.innerHTML = \"\"; }}";
? ? ? ? private static readonly object eventObj = new object( );
? ? ? ? public event FindTargetContainerEventHandler FindTargetContainer
? ? ? ? {
? ? ? ? ? ? add
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Events.AddHandler( eventObj , value );
? ? ? ? ? ? }
? ? ? ? ? ? remove
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Events.RemoveHandler( eventObj , value );
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public AdRotator( )
? ? ? ? ? ? : base( HtmlTextWriterTag.Span )
? ? ? ? {
? ? ? ? }
? ? ? ? [TypeConverter( typeof( ValidatedControlConverter ) )]
? ? ? ? [DefaultValue( "" ) , Category( "Data" ) , Description( "廣告位后期加載后顯示的位置的控件ID" )]
? ? ? ? [IDReferenceProperty]
? ? ? ? public string TargetContainerID
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return (string)this.ViewState[ "TargetContainerID" ] ?? string.Empty;
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.ViewState[ "TargetContainerID" ] = value;
? ? ? ? ? ? }
? ? ? ? }? ? ? ?
? ? ? ? [Bindable( true ) , Category( "Data" ) , DefaultValue( "" ) , Description( "廣告位的默認內容" )]
? ? ? ? public string DefaultContent
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return (string)this.ViewState[ "DefaultContent" ] ?? "廣告位招租";
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.ViewState[ "DefaultContent" ] = value;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? protected override void OnPreRender( EventArgs e )
? ? ? ? {
? ? ? ? ? ? base.OnPreRender( e );
? ? ? ? ? ? if ( !string.IsNullOrEmpty( TargetContainerID ) )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? this.Style.Add( HtmlTextWriterStyle.Display , "none" );
? ? ? ? ? ? ? ? ClientScriptManager cs = Page.ClientScript;
? ? ? ? ? ? ? ? if ( !cs.IsStartupScriptRegistered( this.ClientID ) )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? cs.RegisterStartupScript( this.GetType( ) , this.ClientID , GetLazyLoadingScript( ) , true );
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? protected virtual string GetLazyLoadingScript( )
? ? ? ? {
? ? ? ? ? ? Control target = FindControlHelper( TargetContainerID );
? ? ? ? ? ? if ( target == null )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? throw new TargetContainerNotFoundException( );
? ? ? ? ? ? }
? ? ? ? ? ? StringBuilder sb = new StringBuilder( );
? ? ? ? ? ? sb.AppendFormat( scriptFormat , target.ClientID , this.ClientID );
? ? ? ? ? ? return sb.ToString( );
? ? ? ? }
? ? ? ? protected virtual void RenderAdvertisement( Advertisement ad , HtmlTextWriter writer )
? ? ? ? {
? ? ? ? ? ? // 廣告內容
? ? ? ? }
? ? ? ? protected override void RenderContents( HtmlTextWriter writer )
? ? ? ? {
? ? ? ? ? ? // call RenderAdvertisement method
? ? ? ? }
? ? ? ? protected Control FindControlHelper( string id )
? ? ? ? {
? ? ? ? ? ? Control c = null;
? ? ? ? ? ? if ( findControlHelperCacheList.ContainsKey( id ) )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? c = findControlHelperCacheList[ id ];
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? c = base.FindControl( id );? // 注意:我們從自己開始向上沿NamingContainer層次查找
? ? ? ? ? ? ? ? Control nc = NamingContainer;
? ? ? ? ? ? ? ? while ( ( null == c ) && ( null != nc ) )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? c = nc.FindControl( id );
? ? ? ? ? ? ? ? ? ? nc = nc.NamingContainer;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if ( null == c )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? // 因為我們是從自己開始向上沿NamingContainer層次查找,而沒有向下找,
? ? ? ? ? ? ? ? ? ? // 并且沒有找每一層NamingContainer內的其他NamingContainer,
? ? ? ? ? ? ? ? ? ? // 所以這種查找有可能出現沒有找到id對應的控件
? ? ? ? ? ? ? ? ? ? // 當沒此時有找到時,激發FindTargetContainer事件交給用戶自己設定Target Container Control
? ? ? ? ? ? ? ? ? ? FindTargetContainerEventArgs args = new FindTargetContainerEventArgs( id );
? ? ? ? ? ? ? ? ? ? OnFindTargetContainer( args );
? ? ? ? ? ? ? ? ? ? c = args.Control;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if ( null != c )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? findControlHelperCacheList[ id ] = c;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return c;
? ? ? ? }
? ? ? ? protected virtual void OnFindTargetContainer( FindTargetContainerEventArgs e )
? ? ? ? {
? ? ? ? ? ? FindTargetContainerEventHandler hander = Events[ eventObj ] as FindTargetContainerEventHandler;
? ? ? ? ? ? if ( hander != null )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? hander( this , e );
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public delegate void FindTargetContainerEventHandler( object src , FindTargetContainerEventArgs e );
? ? public class FindTargetContainerEventArgs : EventArgs
? ? {
? ? ? ? private string controlID;
? ? ? ? private Control control;
? ? ? ? public FindTargetContainerEventArgs( string controlId )
? ? ? ? {
? ? ? ? ? ? controlID = controlId;
? ? ? ? }
? ? ? ? public string ControlID
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return controlID;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? public Control Control
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return control;
? ? ? ? ? ? }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? control = value;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? public class TargetContainerNotFoundException : Exception
? ? {
? ? ? ? string exceptionMessage = string.Empty;
? ? ? ? public TargetContainerNotFoundException( )
? ? ? ? ? ? : this( "TargetContainerID所指定的控件沒有找到!您或許應該處理一下FindTargetContainer事件" )
? ? ? ? {
? ? ? ? }
? ? ? ? public TargetContainerNotFoundException( string message )
? ? ? ? ? ? : base( message )
? ? ? ? {
? ? ? ? ? ? this.exceptionMessage = message;
? ? ? ? }
? ? ? ? public override string Message
? ? ? ? {
? ? ? ? ? ? get
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if ( exceptionMessage != null )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return exceptionMessage;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return base.Message;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
http://www.cn-web.com/cnweb/0/411/article/
看不懂的話,先看下asp.net中的NamingContainer詳解
文章來源(WEB開發技術知識庫):http://www.cn-web.com/cnweb/0/411/article/
轉載于:https://www.cnblogs.com/sainaxingxing/archive/2008/09/02/1282401.html
總結
以上是生活随笔為你收集整理的自定义控件:广告内容后期加载。以及NamingContainer层次的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 神奇的事情一再发生
- 下一篇: javascript中使用重载