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.
You can’t have multiple control definitions registered for the same DOM element – you’ll get a script error:
You’ll need to change one or both of your script classes to inherit
Sys.UI.Behaviorinstead ofSys.UI.Control:becomes:
You’ll also need to replace the relevant
ScriptControlDescriptorwith aScriptBehaviorDescriptor:becomes:
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