/// <summary> /// Called by the containing <see cref="UserControl"/> if bindings must be initialized. /// </summary> private void BindingOwner_DataBindingsInitialized(object sender, EventArgs e) { IWebDataBound bindingContainer = (IWebDataBound)sender; if (this.HasControls()) { this.TraverseControls(bindingContainer, this.Controls, new TraversalAction(this.BindControl)); } }
/// <summary> /// Removes custom binding attributes from a webcontrol to avoid them being rendered. /// </summary> private void RemoveBindingAttributes(IWebDataBound dataBound, WebControl wc) { AttributeCollection attributeCollection = wc.Attributes; attributeCollection.Remove(ATTR_BINDINGTARGET); attributeCollection.Remove(ATTR_BINDINGSOURCE); attributeCollection.Remove(ATTR_BINDINGTYPE); attributeCollection.Remove(ATTR_BINDINGDIRECTION); attributeCollection.Remove(ATTR_BINDINGFORMATTER); }
/// <summary> /// Adds all controls on this panel to the containing <see cref="UserControl.BindingManager"/>'s binding collection. /// </summary> /// <param name="bindingContainer">the usercontrol containing this panel</param> /// <param name="controls">the <see cref="ControlCollection"/> of controls to be bound</param> /// <param name="action">action to be performed on matching controls</param> private void TraverseControls(IWebDataBound bindingContainer, ControlCollection controls, TraversalAction action) { foreach (Control control in controls) { // DataBindingPanels must not be nested if (control is DataBindingPanel) { throw new HttpException("Controls of type DataBindingPanel must not be nested"); } // abort recursion on LiteralControl or nested UserControl if (control is LiteralControl || control is UserControl) { continue; } // if it's a WebControl, check for binding-related attributes WebControl wc = control as WebControl; if (wc != null) { try { action(bindingContainer, wc); } catch (Exception ex) { string msg = string.Format("Error executing action on control '{0}' of type '{1}'", wc.UniqueID, wc.GetType().FullName); Log.Error(msg, ex); throw new HttpException(msg, ex); } } if (control.HasControls()) { this.TraverseControls(bindingContainer, control.Controls, action); } } }
/// <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 ); } }
/// <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); } }