多个ScriptControl实例共享变量

时间:2010-10-20 18:51:43

标签: asp.net scope scriptcontrol

我有ScriptControl使用图片作为嵌入资源,GetWebResourceUrl生成WebResource.axd网址。我目前正在使用GetScriptDescriptors()将URL发送到JavaScript对象。

ScriptControl可以在Repeater中,因此您可能有20多个实例。它们都使用相同的图像(不能通过属性自定义),所以我想在变量中设置一次URL并共享它。我知道我可以使用全局变量注册脚本块,但是如果可能的话,我希望避免这种情况。有没有办法在控件类型的范围内设置变量(全局 - > 控件类型 - >控件实例)?

1 个答案:

答案 0 :(得分:0)

这似乎运作良好:

        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.imgDisabledthis.imgEnabled来获取网址。

修改AssemblyInfo.cs中的另一个选项是您将JavaScript引用设置为WebResource(..., PerformSubstitution = true),然后您的.js文件可以随时随地<%= WebResource("MyNamespace.enabled.png") %>。这可以是您实际使用它或添加到对象原型的地方。