开发者

Multiple ScriptControl instances sharing variable

I have a ScriptControl that uses an image as an embedded resource and GetWebResourceUrl to generate the WebResource.axd URL. I am currently using GetScriptDescriptors() to send the URL to the JavaScript object.

The ScriptControl ca开发者_如何学编程n be in a Repeater, so you may have 20+ instances. They all use the same images (not customizable through property), so I would like to set the URL once in a variable and share it. I know I could register a script block with a global variable, but would like to avoid that if possible. Is there a way to set a variable within the scope of the control type (global -> control type -> control instance)?


This seems to work well:

        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);

Then in my object I just do this.imgDisabled or this.imgEnabled to get the URLs.

Edit: Another option is in AssemblyInfo.cs you set the JavaScript reference to WebResource(..., PerformSubstitution = true) then your .js file can have <%= WebResource("MyNamespace.enabled.png") %> anywhere you wish. This can be where you actually use it or adding to the object prototype.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜