两个.js文件的GetScriptDescriptors()方法:奇怪的意外行为

Ant*_*tov 6 javascript asp.net custom-server-controls

我的自定义服务器端ajax控件实现了IScriptControl:

  • GetScriptReferences
  • GetScriptDescriptors

第一种方法发送javascript文件,第二种方法基于一些发送的早期.js文件创建javascript对象.

在我的'AssembleyInfo'文件中,我在Properties explorer中添加了下面的行和标记的.js文件作为'Embedded resourece':

    // this allows access to this files
    [assembly: WebResource("ProjectName.file1.js", "text/javascript")]

    [assembly: WebResource("ProjectName.file2.js", "text/javascript")]
Run Code Online (Sandbox Code Playgroud)

这是IScriptControl的实现:

            public IEnumerable<ScriptReference>  
                GetScriptReferences()
            {
                yield return new ScriptReference("ProjectName.file1.js", this.GetType().Assembly.FullName);
                yield return new ScriptReference("ProjectName.file2.js", this.GetType().Assembly.FullName);
            }

            public IEnumerable<ScriptDescriptor>
                GetScriptDescriptors()
             {


                ScriptControlDescriptor descriptor = new ScriptControlDescriptor("ProjectName.file1", this.ClientID);
                //adding properties and events (I use "AnotherName" on the safe side to avoid potentional namespace problems           
                ScriptControlDescriptor descriptor2 = new ScriptControlDescriptor  ("AnotherName.file2", this.ClientID);
                //adding properties and events   

                yield return descriptor;
                yield return descriptor2;

            }
Run Code Online (Sandbox Code Playgroud)

这是我的.js文件的一部分:

  • 第一个文件

    Type.registerNamespace("ProjectName");
    
    ProjectName.file1 = function (element) {
    .......
    .......
    }
    ProjectName.file1.registerClass('ProjectName.file1', Sys.UI.Control);
    
    if (typeof (Sys) !== 'undefined')
        Sys.Application.notifyScriptLoaded();
    
    Run Code Online (Sandbox Code Playgroud)
  • 第二个档案

    Type.registerNamespace("AnotherName");
    
    AnotherName.file2 = function (element) {
    ............
    ............
    }
    AnotherName.file2.registerClass('AnotherName.file2', Sys.UI.Control);
    
    if (typeof (Sys) !== 'undefined')
        Sys.Application.notifyScriptLoaded();
    
    Run Code Online (Sandbox Code Playgroud)

为什么只创造第一个对象?

    yield return descriptor
Run Code Online (Sandbox Code Playgroud)

我的ASPX必须创建第二个JAVASCRIPT.

如果我评论上面的声明第二次创建通常.

Ric*_*ing 3

您不能为同一个 DOM 元素注册多个控件定义 - 您将收到脚本错误:

Sys.InvalidOperationException:控件已与该元素关联。

您需要将一个或两个脚本类更改为继承,Sys.UI.Behavior而不是Sys.UI.Control

YourType.registerClass("YourType", Sys.UI.Control);
Run Code Online (Sandbox Code Playgroud)

变成:

YourType.registerClass("YourType", Sys.UI.Behavior);
Run Code Online (Sandbox Code Playgroud)

您还需要将相关内容替换ScriptControlDescriptorScriptBehaviorDescriptor

new ScriptControlDescriptor("YourType", ClientID);
Run Code Online (Sandbox Code Playgroud)

变成:

new ScriptBehaviorDescriptor("YourType", ClientID);
Run Code Online (Sandbox Code Playgroud)

有关创建脚本行为的信息,请查看 MSDN 上的扩展器控件演练: http://msdn.microsoft.com/en-us/library/bb386403%28v=vs.100%29.aspx