日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

自定义控件:广告内容后期加载。以及NamingContainer层次的应用

發(fā)布時(shí)間:2025/6/17 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义控件:广告内容后期加载。以及NamingContainer层次的应用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

《轉(zhuǎn)》http://www.pin5i.com/showtopic-11037.html

網(wǎng)站上的廣告內(nèi)容可能會(huì)因加載過(guò)慢而導(dǎo)致整個(gè)網(wǎng)頁(yè)加載過(guò)慢,我們可以考慮將廣告內(nèi)全部放在網(wǎng)頁(yè)最底部,等整個(gè)頁(yè)面加載完畢後再采用javascript使其顯示,考慮給控件一個(gè)TargetContainerID標(biāo)識(shí),控件廣告內(nèi)容將要被顯示的容器ID,然后從控件自己所處的 開始向上查找該ID所指定的控件,(我們只向上找而沒(méi)有向下找,并且沒(méi)有處理某層次的子NamingContainer,所以不一定能夠找到,這里沒(méi)有考慮從Page對(duì)象向下遞歸查找主要考慮為了提高性能),如果沒(méi)有找到,則考慮給用戶一個(gè)事件讓用戶自己處理TargetContainer。

下面的自定義控件對(duì)此實(shí)現(xiàn)了封裝:
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( "廣告位的默認(rèn)內(nèi)容" )]
? ? ? ? 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 )
? ? ? ? {
? ? ? ? ? ? // 廣告內(nèi)容
? ? ? ? }

? ? ? ? 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 )
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? // 因?yàn)槲覀兪菑淖约洪_始向上沿NamingContainer層次查找,而沒(méi)有向下找,
? ? ? ? ? ? ? ? ? ? // 并且沒(méi)有找每一層NamingContainer內(nèi)的其他NamingContainer,
? ? ? ? ? ? ? ? ? ? // 所以這種查找有可能出現(xiàn)沒(méi)有找到id對(duì)應(yīng)的控件

? ? ? ? ? ? ? ? ? ? // 當(dāng)沒(méi)此時(shí)有找到時(shí),激發(fā)FindTargetContainer事件交給用戶自己設(shè)定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所指定的控件沒(méi)有找到!您或許應(yīng)該處理一下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詳解
文章來(lái)源(WEB開發(fā)技術(shù)知識(shí)庫(kù)):http://www.cn-web.com/cnweb/0/411/article/

轉(zhuǎn)載于:https://www.cnblogs.com/sainaxingxing/archive/2008/09/02/1282401.html

總結(jié)

以上是生活随笔為你收集整理的自定义控件:广告内容后期加载。以及NamingContainer层次的应用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。