public static void AddEvent(this ScriptComponentDescriptor descriptor, string name, string value, bool skipWithEmptyOrNullValue) { if (skipWithEmptyOrNullValue && string.IsNullOrEmpty(value)) { return; } descriptor.AddEvent(name, value); }
public static void DescribeComponent(object instance, ScriptComponentDescriptor descriptor, IUrlResolutionService urlResolver, IControlResolver controlResolver) { // validate preconditions if (instance == null) { throw new ArgumentNullException("instance"); } if (descriptor == null) { throw new ArgumentNullException("descriptor"); } if (urlResolver == null) { urlResolver = instance as IUrlResolutionService; } if (controlResolver == null) { controlResolver = instance as IControlResolver; } // describe properties // PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance); PropertyInfo[] properties = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (PropertyInfo prop in properties) { ScriptControlPropertyAttribute propAttr = null; ScriptControlEventAttribute eventAttr = null; string propertyName = prop.Name; System.ComponentModel.AttributeCollection attribs = new System.ComponentModel.AttributeCollection(Attribute.GetCustomAttributes(prop, false)); // Try getting a property attribute propAttr = (ScriptControlPropertyAttribute)attribs[typeof(ScriptControlPropertyAttribute)]; if (propAttr == null || !propAttr.IsScriptProperty) { // Try getting an event attribute eventAttr = (ScriptControlEventAttribute)attribs[typeof(ScriptControlEventAttribute)]; if (eventAttr == null || !eventAttr.IsScriptEvent) { continue; } } // attempt to rename the property/event ClientPropertyNameAttribute nameAttr = (ClientPropertyNameAttribute)attribs[typeof(ClientPropertyNameAttribute)]; if (nameAttr != null && !string.IsNullOrEmpty(nameAttr.PropertyName)) { propertyName = nameAttr.PropertyName; } // determine whether to serialize the value of a property. readOnly properties should always be serialized //bool serialize = true;// prop.ShouldSerializeValue(instance) || prop.IsReadOnly; //if (serialize) //{ // get the value of the property, skip if it is null Control c = null; object value = prop.GetValue(instance, new object[0] { }); if (value == null) { continue; } // convert and resolve the value if (eventAttr != null && prop.PropertyType != typeof(String)) { throw new InvalidOperationException("ScriptControlEventAttribute can only be applied to a property with a PropertyType of System.String."); } else { if (!prop.PropertyType.IsPrimitive && !prop.PropertyType.IsEnum) { if (prop.PropertyType == typeof(Color)) { value = ColorTranslator.ToHtml((Color)value); } else { // TODO: Determine if we should let ASP.NET AJAX handle this type of conversion, as it supports JSON serialization //TypeConverter conv = prop.Converter; //value = conv.ConvertToString(null, CultureInfo.InvariantCulture, value); //if (prop.PropertyType == typeof(CssStyleCollection)) // value = (new CssStyleCollectionJSCoverter()).Serialize(value, new JavaScriptSerializer()); //if (prop.PropertyType == typeof(Style)) // value = (new CssStyleCollectionJSCoverter()).Serialize(((Style)value).GetStyleAttributes(null), new JavaScriptSerializer()); Type valueType = value.GetType(); JavaScriptConverterAttribute attr = (JavaScriptConverterAttribute)attribs[typeof(JavaScriptConverterAttribute)]; JavaScriptConverter converter = attr != null ? (JavaScriptConverter)TypeCreator.CreateInstance(attr.ConverterType) : JsonSerializerFactory.GetJavaScriptConverter(valueType); if (converter != null) { value = converter.Serialize(value, JsonSerializerFactory.GetJavaScriptSerializer()); } else { value = JsonHelper.PreSerializeObject(value); } //Dictionary<string, object> dict = value as Dictionary<string, object>; //if (dict != null && !dict.ContainsKey("__type")) // dict["__type"] = valueType.AssemblyQualifiedName; } } if (attribs[typeof(IDReferencePropertyAttribute)] != null && controlResolver != null) { c = controlResolver.ResolveControl((string)value); } if (attribs[typeof(UrlPropertyAttribute)] != null && urlResolver != null) { value = urlResolver.ResolveClientUrl((string)value); } } // add the value as an appropriate description if (eventAttr != null) { if (!string.IsNullOrEmpty((string)value)) { descriptor.AddEvent(propertyName, (string)value); } } else if (attribs[typeof(ElementReferenceAttribute)] != null) { if (c == null && controlResolver != null) { c = controlResolver.ResolveControl((string)value); } if (c != null) { value = c.ClientID; } descriptor.AddElementProperty(propertyName, (string)value); } else if (attribs[typeof(ComponentReferenceAttribute)] != null) { if (c == null && controlResolver != null) { c = controlResolver.ResolveControl((string)value); } if (c != null) { //ExtenderControlBase ex = c as ExtenderControlBase; //if (ex != null && ex.BehaviorID.Length > 0) // value = ex.BehaviorID; //else value = c.ClientID; } descriptor.AddComponentProperty(propertyName, (string)value); } else { if (c != null) { value = c.ClientID; } descriptor.AddProperty(propertyName, value); } } //} // determine if we should describe methods foreach (MethodInfo method in instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)) { ScriptControlMethodAttribute methAttr = (ScriptControlMethodAttribute)Attribute.GetCustomAttribute(method, typeof(ScriptControlMethodAttribute)); if (methAttr == null || !methAttr.IsScriptMethod) { continue; } // We only need to support emitting the callback target and registering the WebForms.js script if there is at least one valid method Control control = instance as Control; if (control != null) { // Force WebForms.js control.Page.ClientScript.GetCallbackEventReference(control, null, null, null); // Add the callback target descriptor.AddProperty("_callbackTarget", control.UniqueID); } break; } }
protected override void BuildScriptDescriptor(ScriptComponentDescriptor descriptor) { base.BuildScriptDescriptor(descriptor); descriptor.AddComponentProperty("DataSource", this.DataSourceID, this); descriptor.AddEvent("itemActivated", this.OnClientItemActivated, true); descriptor.AddEvent("itemUpdated", this.OnClientItemUpdated, true); descriptor.AddEvent("selectionChanged", this.OnClientSelectionChanged, true); descriptor.AddProperty("EmptyDataText", this.EmptyDataText); descriptor.AddProperty("CaptionText", this.CaptionText, true); if (this.ProgressDelay != 0) { descriptor.AddProperty("ProgressDelay", this.ProgressDelay); } if (this.Features != 0) { descriptor.AddProperty("Features", this.Features); } if (this.SortDirection != SortDirection.Ascending) { descriptor.AddProperty("SortDirection", this.SortDirection); } descriptor.AddProperty("SortProperty", this.SortProperty, true); if (this.IdentityProperty != "Identity") { descriptor.AddProperty("IdentityProperty", this.IdentityProperty); } if (this.PreLoad) { descriptor.AddProperty("PreLoadResultsString", this.preLoadResults.ToJsonString(null)); } if (this.NameProperty != "Name") { descriptor.AddProperty("NameProperty", this.NameProperty); } if (this.InlineEditMaxLength != 128) { descriptor.AddProperty("InlineEditMaxLength", this.InlineEditMaxLength); } if (this.SupportAsyncGetList) { descriptor.AddProperty("PageSize", ListView.pageSize, 500); } else { descriptor.AddProperty("PageSize", ListView.pageSizeWithNoPaging, 3000); } descriptor.AddProperty("PageSizes", ListView.pageSizes, true); StringBuilder stringBuilder = new StringBuilder("["); stringBuilder.Append(string.Join(",", from o in this.Columns select o.ToJavaScript())); stringBuilder.Append("]"); descriptor.AddScriptProperty("Columns", stringBuilder.ToString()); if (this.toolbarPanel != null && this.ShowToolBar) { descriptor.AddComponentProperty("ToolBar", this.toolbar.ClientID); } if (this.viewFilterDropDown != null) { descriptor.AddComponentProperty("ViewFilterDropDown", this.viewFilterDropDown.ClientID); } if (this.searchTextBox != null && this.ShowSearchBar) { descriptor.AddComponentProperty("SearchTextBox", this.searchTextBox.ClientID); } if (this.listViewInputPanel != null && this.IsEditable) { descriptor.AddComponentProperty("InputTextBox", this.listViewInputPanel.ClientID); } }
public static void DescribeComponent(object instance, ScriptComponentDescriptor descriptor, IUrlResolutionService urlResolver, IControlResolver controlResolver) { // validate preconditions if (instance == null) { throw new ArgumentNullException("instance"); } if (descriptor == null) { throw new ArgumentNullException("descriptor"); } if (urlResolver == null) { urlResolver = instance as IUrlResolutionService; } if (controlResolver == null) { controlResolver = instance as IControlResolver; } // describe properties PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance); foreach (PropertyDescriptor prop in properties) { ExtenderControlPropertyAttribute propAttr = null; ExtenderControlEventAttribute eventAttr = null; string propertyName = prop.Name; // Try getting a property attribute propAttr = (ExtenderControlPropertyAttribute)prop.Attributes[typeof(ExtenderControlPropertyAttribute)]; if (propAttr == null || !propAttr.IsScriptProperty) { // Try getting an event attribute eventAttr = (ExtenderControlEventAttribute)prop.Attributes[typeof(ExtenderControlEventAttribute)]; if (eventAttr == null || !eventAttr.IsScriptEvent) { continue; } } // attempt to rename the property/event ClientPropertyNameAttribute nameAttr = (ClientPropertyNameAttribute)prop.Attributes[typeof(ClientPropertyNameAttribute)]; if (!string.IsNullOrEmpty(nameAttr.PropertyName)) { propertyName = nameAttr.PropertyName; } object value = prop.GetValue(instance); if (value == null) { continue; } // determine whether to serialize the value of a property. readOnly properties should always be serialized bool serialize = prop.ShouldSerializeValue(instance) || prop.IsReadOnly; if (serialize) { // get the value of the property, skip if it is null Control c = null; // convert and resolve the value if (eventAttr != null && prop.PropertyType != typeof(String)) { throw new InvalidOperationException( "ExtenderControlEventAttribute can only be applied to a property with a PropertyType of System.String."); } else { if (!prop.PropertyType.IsPrimitive && !prop.PropertyType.IsEnum) { // Check if we can use any of our custom converters // (first do a direct lookup on the property type, // but also check all of its base types if nothing // was found) Converter <object, string> customConverter = null; if (!_customConverters.TryGetValue(prop.PropertyType, out customConverter)) { foreach (KeyValuePair <Type, Converter <object, string> > pair in _customConverters) { if (prop.PropertyType.IsSubclassOf(pair.Key)) { customConverter = pair.Value; break; } } } // Use the custom converter if found, otherwise use // its current type converter if (customConverter != null) { value = customConverter(value); } else { // Determine if we should let ASP.NET AJAX handle this type of conversion, as it supports JSON serialization if (propAttr != null && propAttr.UseJsonSerialization) { if (value is IEnumerable) { List <object> lst = new List <object>(); foreach (object a in (IEnumerable)value) { object b; Type typ = a.GetType(); if (a as IUICollectionItem != null) { b = a is UriValue ? urlResolver.ResolveClientUrl(((UriValue)a).Value.ToString()) : ((IUICollectionItem)a).Value; } else if (typ.IsPrimitive || typ.IsValueType) { b = a; } else { b = BuildGraph(a, urlResolver, controlResolver); } if (b != null) { lst.Add(b); } } if (lst.Count > 0) { value = lst; } } else if (value.GetType().IsPrimitive || value.GetType().IsValueType) { } else { value = BuildGraph(value, urlResolver, controlResolver); } // Use ASP.NET JSON serialization } else { // Use the property's own converter TypeConverter conv = prop.Converter; value = conv.ConvertToString(null, CultureInfo.InvariantCulture, value); } } } if (prop.Attributes[typeof(IDReferencePropertyAttribute)] != null && controlResolver != null) { c = controlResolver.ResolveControl((string)value); } if (prop.Attributes[typeof(UrlPropertyAttribute)] != null && urlResolver != null) { value = urlResolver.ResolveClientUrl((string)value); } } // add the value as an appropriate description if (eventAttr != null) { descriptor.AddEvent(propertyName, (string)value); } else if (prop.Attributes[typeof(ElementReferenceAttribute)] != null) { if (c == null && controlResolver != null) { c = controlResolver.ResolveControl((string)value); } if (c != null) { value = c.ClientID; } descriptor.AddElementProperty(propertyName, (string)value); } else if (prop.Attributes[typeof(ComponentReferenceAttribute)] != null) { if (c == null && controlResolver != null) { c = controlResolver.ResolveControl((string)value); } if (c != null) { ExtenderControlBase ex = c as ExtenderControlBase; if (ex != null && ex.BehaviorID.Length > 0) { value = ex.BehaviorID; } else { value = c.ClientID; } } descriptor.AddComponentProperty(propertyName, (string)value); } else { if (c != null) { value = c.ClientID; } descriptor.AddProperty(propertyName, value); } } } // determine if we should describe methods foreach ( MethodInfo method in instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)) { ExtenderControlMethodAttribute methAttr = (ExtenderControlMethodAttribute) Attribute.GetCustomAttribute(method, typeof(ExtenderControlMethodAttribute)); if (methAttr == null || !methAttr.IsScriptMethod) { continue; } // We only need to support emitting the callback target and registering the WebForms.js script if there is at least one valid method Control control = instance as Control; if (control != null) { // Force WebForms.js control.Page.ClientScript.GetCallbackEventReference(control, null, null, null); // Add the callback target descriptor.AddProperty("_callbackTarget", control.UniqueID); } break; } }
public void AddEvent(string name, string handler) { _descriptor.AddEvent(name, handler); }