Wednesday, March 28, 2012

Programmatic equivalent to System.Web.UI.ScriptResource attribute?

Is there a programmatic equivalent to the following attribute normally put in the AssemblyInfo file in a class library project?

<Assembly: System.Web.UI.ScriptResource("LocalizedControl.TravelInfo.js", "LocalizedControl.TravelResources", "Travel")>

I would like to use reflection to do this programmatically w/o having to link in System.Web.Extensions.

I can use ScriptManager.RegisterClientScriptResource()

to do the equivalent of

<Assembly: System.Web.UI.WebResource("LocalizedControl.TravelInfo.js", "application/x-javascript")>

but have not found a way to do the equivalent of the ScriptResource attribute.

Thanks,
Chris

Hi cpels,

Hope this will helps.

To specify how resource files are managed when an assembly is built, you include attributes in the AssemblyInfo file (AssemblyInfo.vb or AssemblyInfo.cs file).

In ASP.NET, you mark resources for the application by using theWebResourceAttribute class. To embed JavaScript files in an assembly, you use this attribute to specify the .js files as a Web resource.

To include resource files for the embedded JavaScript files, you use theScriptResourceAttribute class that is provided by ASP.NET AJAX. This attribute identifies the files specifically as resources for the JavaScript files.

The following example shows how to use assembly attributes to identify embedded scripts and their associated script resources.

' Indicates that neutral fallback resources are retrieved from ' the main assembly named MainAssembly.<assembly: NeutralResourcesLanguageAttribute("en-US", UltimateResourceFallbackLocation.MainAssembly)>' Defines embedded scripts as Web resources.<assembly:WebResource("Sample.js", "text/javascript")><assembly:WebResource("Sample.debug.js", "text/javascript")>' Defines the script resources for the scripts and their types.<assembly:ScriptResource("Sample.js", "Sample.resources", "Sample.Res")><assembly:ScriptResource("Sample.debug.js", "Sample.debug.resources", "Sample.Res")>

// Indicates that neutral fallback resources are retrieved from // the main assembly named MainAssembly.[assembly: NeutralResourcesLanguageAttribute("en-US", UltimateResourceFallbackLocation.MainAssembly)]// Defines embedded scripts as Web resources.[assembly:WebResource("Sample.js", "text/javascript")][assembly:WebResource("Sample.debug.js", "text/javascript")]// Defines the script resources for the scripts and their types.[assembly:ScriptResource("Sample.js", "Sample.resources", "Sample.Res")][assembly:ScriptResource("Sample.debug.js", "Sample.debug.resources", "Sample.Res")]
 

In this example, a main assembly namedMainAssembly contains an embedded release version of a client script file that is named Sample.js. The assembly also contains the corresponding debug version named Sample.debug.js. The .js files are identified as resources by theWebResourceAttribute attribute.

TheNeutralResourcesLanguageAttribute assembly attribute is used to specify the main assembly as the fallback culture. For more information, seeNeutral Resources Languages for Localization and theNeutralResourcesLanguageAttribute class overview.

The resources used by the script files are defined by using theScriptResourceAttribute attribute. The Sample.resources and Sample.debug.resources files contain resource values for the Sample.js and Sample.debug.js files, respectively.

A type named Sample.Res is generated for both the release and the debug version of the script resources. This is the type that the JavaScript code should use to access localized values. For both release mode and debug mode, the build process creates only a single type. In debug mode the resources for the release version are combined with the additional resources for the debug version.

For more information about how to create assembly-information files and the assembly metadata that is required for versioned assemblies, seeHow to: Create Versioned Assemblies for Precompiled Web Sites.

http://ajax.asp.net/docs/overview/LocalizingResources.aspx
If I misunderstood you,please let me know.
 


I am familiar with how to use the ScriptResourceAttribute in an assembly. However, this requires that the System.Web.Extensions assembly be referenced in the assembly that I am building. What I want to do is NOT have my assembly reference System.Web.Extensions, but rather use reflection to invoke various aspects of MSFT AJAX so there is no dependency on having AJAX installed. This allows my user control in the assembly to work on systems that are not using AJAX if necessary.

I have been able to implement all aspects of my MSFT AJAX functionality this way except the ScriptResourceAttribute. The ScriptManager class has the RegisterClientScriptResource() method. However, the ScriptManager class does not have a method for associating a resource with a client script the way the ScriptResourceAttribute does.

Chris

No comments:

Post a Comment