我的自定义服务器端ajax控件实现了IScriptControl:
第一种方法发送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")]
这是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。
如果我通常发表声明第二次创建。
答案 0 :(得分:3)
您不能为同一个DOM元素注册多个控件定义 - 您将收到脚本错误:
Sys.InvalidOperationException:控件已与元素关联。
您需要更改一个或两个脚本类以继承Sys.UI.Behavior
而不是Sys.UI.Control
:
YourType.registerClass("YourType", Sys.UI.Control);
变为:
YourType.registerClass("YourType", Sys.UI.Behavior);
您还需要将相关的ScriptControlDescriptor
替换为ScriptBehaviorDescriptor
:
new ScriptControlDescriptor("YourType", ClientID);
变为:
new ScriptBehaviorDescriptor("YourType", ClientID);
有关创建脚本行为的信息,请查看MSDN上的扩展程序控件演练: http://msdn.microsoft.com/en-us/library/bb386403%28v=vs.100%29.aspx
答案 1 :(得分:0)
似乎您的问题源于您使用TWO返回
的事实yield return descriptor;
yield return descriptor2;
第一个返回然后结束,你不能拥有一个有两个返回的函数。尝试创建IEnumerable集合,将描述符放在里面,然后返回整个东西。
List<ScriptDescriptor> descList = new List<ScriptDescriptor>();
descList.add(descriptor);
descList.add(descriptor2);
return descList;