private static IBinding CreateBindingInstance(string bindingTypeName, string bindingSource, string bindingTarget, BindingDirection bindingDirection, IFormatter bindingFormatter) { IBinding binding; Type bindingType = TypeResolutionUtils.ResolveType(bindingTypeName); ConstructorInfo ctor = bindingType.GetConstructor(new Type[] { typeof(string), typeof(string), typeof(BindingDirection), typeof(IFormatter) }); if (ctor == null) { throw new ArgumentException(string.Format("Specified BindingType '{0}' does not implement constructor (string,string,BindingDirection,IFormatter)", bindingTypeName)); } binding = (IBinding) ObjectUtils.InstantiateType(ctor, new object[] { bindingSource, bindingTarget, bindingDirection, bindingFormatter }); return(binding); }
private static IBinding CreateBindingInstance(string bindingTypeName, string bindingSource, string bindingTarget, BindingDirection bindingDirection, IFormatter bindingFormatter) { IBinding binding; Type bindingType = TypeResolutionUtils.ResolveType(bindingTypeName); ConstructorInfo ctor = bindingType.GetConstructor(new Type[] {typeof(string), typeof(string), typeof(BindingDirection), typeof(IFormatter)}); if (ctor == null) { throw new ArgumentException(string.Format("Specified BindingType '{0}' does not implement constructor (string,string,BindingDirection,IFormatter)",bindingTypeName)); } binding = (IBinding) ObjectUtils.InstantiateType(ctor, new object[] {bindingSource, bindingTarget, bindingDirection, bindingFormatter}); return binding; }
/// <summary> /// Retrieves custom binding attributes from a webcontrol and adds a new binding /// instance to the container's <see cref="UserControl.BindingManager"/> if necessary. /// </summary> private void BindControl(IWebDataBound dataBound, WebControl theControl) { // special handling of adapted controls DataBindingAdapter adapterControl = theControl as DataBindingAdapter; Control wc = (adapterControl != null && adapterControl.WrappedControl != null) ? adapterControl.WrappedControl : theControl; AttributeCollection attributeCollection = theControl.Attributes; string bindingTarget = attributeCollection[ATTR_BINDINGTARGET]; // at least a BindingTarget must be specified if (bindingTarget == null) { return; } attributeCollection.Remove(ATTR_BINDINGTARGET); // determine direction BindingDirection bindingDirection = BindingDirection.Bidirectional; string strBindingDirection = attributeCollection[ATTR_BINDINGDIRECTION]; if (strBindingDirection != null) { bindingDirection = (BindingDirection)Enum.Parse(typeof(BindingDirection), strBindingDirection); } // determine BindingSource string bindingSource = attributeCollection[ATTR_BINDINGSOURCE]; if (bindingSource == null) { bindingSource = AutoProbeSourceProperty(wc); } attributeCollection.Remove(ATTR_BINDINGSOURCE); // get formatter if any IFormatter bindingFormatter = null; string bindingFormatterName = attributeCollection[ATTR_BINDINGFORMATTER]; if (bindingFormatterName != null) { bindingFormatter = (IFormatter)dataBound.ApplicationContext.GetObject(bindingFormatterName); attributeCollection.Remove(ATTR_BINDINGFORMATTER); } // determine source expression string containerName = dataBound.UniqueID; string controlName = wc.UniqueID; string relativeControlName = null; if (dataBound is System.Web.UI.Page) { relativeControlName = string.Format("FindControl('{0}')", controlName); } else if ((Control)dataBound != this.NamingContainer) { relativeControlName = (controlName.StartsWith(containerName)) ? controlName.Substring(containerName.Length + 1) : controlName; relativeControlName = string.Format("FindControl('{0}')", relativeControlName); } else { relativeControlName = wc.ID; } // if no bindingSource, expression evaluates to the bound control bindingSource = (StringUtils.HasLength(bindingSource)) ? relativeControlName + "." + bindingSource : relativeControlName; Log.Debug( string.Format("binding control '{0}' relative to '{1}' using expression '{2}'", controlName, containerName, bindingSource)); //get bindingType if any IBinding binding = null; string bindingTypeName = attributeCollection[ATTR_BINDINGTYPE]; if (bindingTypeName == null) { bindingTypeName = AutoProbeBindingType(wc); } // get messageId and errorProviders list string messageId = attributeCollection[ATTR_MESSAGEID]; string errorProvidersText = attributeCollection[ATTR_ERRORPROVIDERS]; string[] errorProviders = null; if (StringUtils.HasLength(errorProvidersText)) { errorProviders = (string[])new Spring.Core.TypeConversion.StringArrayConverter().ConvertFrom(errorProvidersText); } // add binding to BindingManager if (bindingTypeName != null) { binding = CreateBindingInstance(bindingTypeName, bindingSource, bindingTarget, bindingDirection, bindingFormatter); binding = dataBound.BindingManager.AddBinding(binding); } else { binding = dataBound.BindingManager.AddBinding(bindingSource, bindingTarget, bindingDirection, bindingFormatter); } // set error message if (StringUtils.HasLength(messageId)) { binding.SetErrorMessage(messageId, errorProviders); } }