多个ScriptControl实例共享变量
-
09-10-2019 - |
题
我有一个 ScriptControl
它使用图像作为嵌入式资源, GetWebResourceUrl
生成 WebResource.axd
URL。我目前正在使用 GetScriptDescriptors()
将URL发送到JavaScript对象。
这 ScriptControl
可以在 Repeater
, ,因此您可能有20多个实例。他们都使用相同的图像(无法通过属性自定义),因此我想将URL设置在变量中并共享。我知道我可以用全局变量注册一个脚本块,但希望在可能的情况下避免。是否有一种方法可以在控制类型的范围内设置变量(global->> 控制类型 - >控制实例)?
解决方案
这似乎很好:
string imagesScript = String.Format(
"MyNamespace.MyClass.prototype.imgDisabled = '{0}';" +
"MyNamespace.MyClass.prototype.imgEnabled = '{1}';",
Page.ClientScript.GetWebResourceUrl(typeof(MyClass), "MyNamespace.disabled.png"),
Page.ClientScript.GetWebResourceUrl(typeof(MyClass), "MyNamespace.enabled.png")
);
Page.ClientScript.RegisterStartupScript(typeof(MyClass), "Images", imagesScript, true);
然后在我的对象中,我只是做 this.imgDisabled
或者 this.imgEnabled
获取URL。
编辑: 另一个选择是 AssemblyInfo.cs
您将JavaScript引用设置为 WebResource(..., PerformSubstitution = true)
然后您的.js文件可以 <%= WebResource("MyNamespace.enabled.png") %>
您想要的任何地方。这可以是您实际使用它或添加到对象原型的地方。
不隶属于 StackOverflow