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

My ajax user server control implements IScriptControl:

  • GetScriptReferences
  • GetScriptDescriptors

The first method sends javascript files, the second creates javascript objects based on some send files earlier .js.

In my "AssembleyInfo" file, I added the lines and marked .js files in the "Properties" in the "Properties":

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

Here is the 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 are 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 THE FIRST OBJECT?

  yield return descriptor 

My ASPX has JAVASCRIPT, WHICH SHOULD CREATE A SECOND.

IF I COMMENT ABOVE THE SECOND STATEMENT, THE SECOND CREATES NORMALLY.

+6
source share
2 answers

You cannot have multiple definition definitions registered for the same DOM element - you will get a script error:

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

You will need to modify one or both 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 will also need to replace the corresponding ScriptControlDescriptor with a ScriptBehaviorDescriptor :

 new ScriptControlDescriptor("YourType", ClientID); 

becomes:

 new ScriptBehaviorDescriptor("YourType", ClientID); 

Take a look at going through Extender management on MSDN for information on creating a script behavior: http://msdn.microsoft.com/en-us/library/bb386403%28v=vs.100%29.aspx

+3
source

Your problem seems to be related to the fact that you are using TWO return

 yield return descriptor; yield return descriptor2; 

The return is performed first, and then its completion, you do not have a function that has two types of return. Try creating an IEnumerable collection, place the descriptors inside, and then return everything.

 List<ScriptDescriptor> descList = new List<ScriptDescriptor>(); descList.add(descriptor); descList.add(descriptor2); return descList; 
0
source

All Articles