【问题标题】:Write ClientSide code from Custom ASP .NET Extender server side ExtenderControlBase从自定义 ASP .NETExtender 服务器端 ExtenderControlBase 编写客户端代码
【发布时间】:2012-07-18 18:27:28
【问题描述】:

我正在 ASP .NET 中扩展 ExtenderControlBase 控件。它被称为:

public class LookupExtender : ExtenderControlBase

基本上它在类似于自动完成功能的功能中所做的 - 但是静态的。 LookupExtender 具有 TypeNameListName 属性,它们指定:

  • 具有string[] GetList(string listName)方法的类
  • 将传递给 GetList 方法的列表名称

现在,LookupExtender 动态创建 TypeName 实例(反射),调用 GetList 方法,我想渲染 string[]结果以数组的形式发送到客户端,以便扩展器客户端代码具有自动建议的静态源。

有没有办法从 LookupExtender 类渲染 JavaScript?

这是我的示例代码(当前自动建议值是硬编码的):

set_TargetTextBoxID: function (value) {
    this._targetTextBoxID = value;

    $(function () {
        var availableTags = [
            "Switzerland",
            "Poland",
            "Europe",
            "USA",
            "Asia"
        ];
        $("#" + value).autocomplete({
            source: availableTags,
            minLength: 0,
            close: function () {
                $(this).blur();
            }
        }).focus(function () {
            $(this).autocomplete("search", "");
        });
    });
}

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    其实很简单

    • 扩展器的服务器端:

      protected override void RenderInnerScript(ScriptBehaviorDescriptor descriptor)
      {
          var listSource = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(SourceType) as ILookupExtenderListSource;
          if (listSource == null) return;
      
          var lookupList = listSource.GetList(SourceList);
          descriptor.AddProperty("LookupArray", lookupList);
      }
      
    • 客户端js脚本:

    CIPNet.Extenders.LookupBehavior.prototype = { 初始化:函数(){ CIPNet.Extenders.LookupBehavior.callBaseMethod(this, 'initialize'); },

    dispose: function() {
        CIPNet.Extenders.LookupBehavior.callBaseMethod(this, 'dispose');
    },
    
    //
    // Property accessors 
    //
    
    get_LookupArray: function() {
        return this._lookupArray;
    },
    
    set_LookupArray: function(value) {
        this._lookupArray = value;
    },
    
    get_TargetTextBoxID: function() {
        return this._targetTextBoxID;
    },
    
    set_TargetTextBoxID: function(value) {
        this._targetTextBoxID = value;
        var that = this;
    
        $(function() {
    
            var showSuggestion = function () {
                $(this).autocomplete("search", "");
            };
    
            var availableTags = that._lookupArray;
            $("#" + value).autocomplete({
                source: availableTags,
                delay: 0,
                minLength: 0
            }).click(showSuggestion);
        });
    }
    

    };

    【讨论】:

      猜你喜欢
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      相关资源
      最近更新 更多