How to Find the Client Behavior of the Extender which is placed inside the DataBind Control

We all know the Extenders of the AjaxControlToolkit implement their Client Partial-Refresh action by using their Client behaviors which are generated after they are rendered. In addition, the client classes provide many useful APIs that enable you to bind to events and to provide handlers for those events.

For example, the AutoComplete Extender's client behavior document would like this:

The AutoCompleteExtender

ContractedBlock.gif ExpandedBlockStart.gif Code
        <AjaxControlToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
            TargetControlID
="TextBox2" ServicePath="AutoComplete1.asmx" ServiceMethod="GetCompletionListByRandom"
            MinimumPrefixLength
="2" CompletionInterval="10" EnableCaching="true" OnClientPopulated="acePopulated"
            OnClientItemSelected
="IAmSelected" />

Its Client Behaviior Document

You may notice that we simply used this method "$find(the Extender's BehaviorID)" to get the Client Behavior Document. If the BehaviorId is not defined, we can use this "$find(<%=the Extender's ClientID%>)".

But sometimes things are not so simple. If we place the Extender inside a DataBind Control's ItemTemplete, such as GridView. The same method would raise error which sames like this:

Sys.InvalidOperationException: Two components with the same id 'XXXXXBehavior1' can't be added to the application.

That's because the BehaviorId shoule be unique.

Then, how can we get the Behaviors in this situation?

Actually, every behavior has been cached into the Sys.Application's Components object after the page client initialize phase in the Life-Cycle. When script in a load event handler runs, all scripts and components have been loaded and are available. That's the key to achieve our goal.

ContractedBlock.gif ExpandedBlockStart.gif Code
        function pageLoad() {
        
//If any Extender is placed in the DataBind Control, it's hard to define the BehaviorId and get the Client behavior. 
        //We can use the method to find all the correct type behaviors.
            var currentBehavior = null;
            
var allBehaviors = Sys.Application.getComponents();
            
for (var loopIndex = 0; loopIndex < allBehaviors.length; loopIndex++) {
                currentBehavior 
= allBehaviors[loopIndex];
                
if (currentBehavior.get_name() == "AutoCompleteBehavior") {
                    
// Now we get the ClientBehavior here: currentBehavior!
                }
            }
        }

 

 

转载于:https://www.cnblogs.com/Zhi-QiangNi/archive/2009/01/15/1376381.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值