我的自定义服务器端 ajax 控件实现 IScriptControl :
- 获取脚本参考
- 获取脚本描述符
第一种方法发送 javascript 文件,第二种方法基于一些较早发送的 .js 文件创建 javascript 对象。
在我的“AssembleyInfo”文件中,我添加了以下行并将属性资源管理器中的 .js 文件标记为“嵌入式资源”:
// this allows access to this files
[assembly: WebResource("ProjectName.file1.js", "text/javascript")]
[assembly: WebResource("ProjectName.file2.js", "text/javascript")]
这是 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;
}
这是我的 .js 文件的一部分:
第一个文件
Type.registerNamespace("ProjectName"); ProjectName.file1 = function (element) { ....... ....... } ProjectName.file1.registerClass('ProjectName.file1', Sys.UI.Control); if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
第二个文件
Type.registerNamespace("AnotherName"); AnotherName.file2 = function (element) { ............ ............ } AnotherName.file2.registerClass('AnotherName.file2', Sys.UI.Control); if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
为什么只创建第一个对象?
yield return descriptor
我的 ASPX 有必须创建第二个的 JAVASCRIPT。
如果我在上面发表评论,则第二次创建正常。