I've updated the Ajax library and added some more functionality.
Thanks to Michael Schwartz for pointing out a security bug - when invoking a method it wasn't checked to see if it had the JavaScriptAccessableMethodAttribute attribute. He also pointed out that DateTime and TimeSpan were not handeled properly - this is also corrected.
Added functionality:
You can now write your own Object Type to JavaScript convert and register it which then overrides the conversion the type from the default.
Converters must implement the IAjaxJavaScriptObjectConverter interface.
public interface IAjaxJavaScriptObjectConverter { void ToJavaScript(ref StringBuilder sb, object obj); /// <summary> /// Converter can be used for multipal Types /// Returns the FullName of the types /// </summary> string[] TypesToConvert{get;} }
I've included StringCollection and DataTable as examples in the demo project. You must register a converter during the loading of the Page or UserControl - just like registering a Type for exporting.
Ajax.JavaScriptHelper.RegisterConverter(new StringCollectionConverter()); Ajax.JavaScriptHelper.RegisterConverter(new DataTableConverter());
Below is the converter for StringCollection - instred of recreating the object it converts it into an array of strings.
public class StringCollectionConverter : Ajax.IAjaxJavaScriptObjectConverter { public StringCollectionConverter() { } #region IAjaxJavaScriptObjectConverter Members public void ToJavaScript(ref StringBuilder sb, object obj) { StringCollection coll = obj as StringCollection; if( coll == null ) { return; } // output as array sb.Append('['); for(int i=0; i<coll.Count-1; i++) { Ajax.JavaScriptHelper.ObjectToJavaScript(ref sb, coll[i] ); sb.Append(','); } if( coll.Count > 0 ) { Ajax.JavaScriptHelper.ObjectToJavaScript(ref sb, coll[coll.Count-1]); } sb.Append(']'); } public string[] TypesToConvert { get{return new string[]{"System.Collections.Specialized.StringCollection"};} } #endregion }
download AJAX for APS.Net
Remember Me