GetScriptDescriptors() method for two .js files : strange unexpected behavior

asp.net, custom-server-controls, javascript

Solution

You can't have multiple control definitions registered for the same DOM element - you'll get a script error:

Sys.InvalidOperationException: A control is already associated with the element.

You'll need to change one or both of your script classes to inherit `Sys.UI.Behavior` instead of `Sys.UI.Control`:

YourType.registerClass("YourType", Sys.UI.Control);

becomes:

YourType.registerClass("YourType", Sys.UI.Behavior);

You'll also need to replace the relevant `ScriptControlDescriptor` with a `ScriptBehaviorDescriptor`:

new ScriptControlDescriptor("YourType", ClientID);

becomes:

new ScriptBehaviorDescriptor("YourType", ClientID);

Have a look at the extender control walk-through on MSDN for information on creating script behaviors: http://msdn.microsoft.com/en-us/library/bb386403%28v=vs.100%29.aspx

Problem

My custom server-side ajax control implements IScriptControl : - GetScriptReferences - GetScriptDescriptors First method sends javascript files,second creates javascript objects based on some sended earlier .js files. In my 'AssembleyInfo' file I added below lines and marked .js files in Properties explorer as 'Embedded resourece' : ``` // this allows access to this files [assembly: WebResource("ProjectName.file1.js", "text/javascript")] [assembly: WebResource("ProjectName.file2.js", "text/javascript")] ``` Here is implementation of 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; } ``` Here is parts of my .js files: first file ``` Type.registerNamespace("ProjectName"); ProjectName.file1 = function (element) { ....... ....... } ProjectName.file1.registerClass('ProjectName.file1', Sys.UI.Control); if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); ``` second file ``` Type.registerNamespace("AnotherName"); AnotherName.file2 = function (element) { ............ ............ } AnotherName.file2.registerClass('AnotherName.file2', Sys.UI.Control); if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); ``` WHY CREATES ONLY FIRST OBJECT? ``` yield return descriptor ``` MY ASPX HAS JAVASCRIPT THAT MUST CREATE SECOND. IF I COMMENT ABOVE STATEMENT SECOND CREATES NORMALLY.

Original source