Example #1
0
        //private static void SetDefaultPropertyOnProducer(ElementCompileTreeNode element, PropertyCompileTreeNode property, CompileContext compileContext)
        //{
        //    string propertyName = GetDefaultPropertyNameOnProducer(element);

        //    SetPropertyOnProducer(element, propertyName, property, compileContext);
        //}



        private static string GetDefaultPropertyNameOnProducer(ElementCompileTreeNode element)
        {
            Type producerType = element.Producer.GetType();

            ControlValuePropertyAttribute cvpa = Attribute.GetCustomAttribute(producerType, typeof(ControlValuePropertyAttribute)) as ControlValuePropertyAttribute;

            if (null == cvpa)
            {
                throw new FormCompileException(string.Format("The producer {0} does not have a default property specified", producerType.ToString()), element.XmlSourceNodeInformation);
            }

            return(cvpa.PropertyName);
        }
Example #2
0
        /// <devdoc>
        /// Returns the updated value of the parameter.
        /// </devdoc>
        protected internal override object Evaluate(HttpContext context, Control control)
        {
            if (control == null)
            {
                return(null);
            }

            string controlID    = ControlID;
            string propertyName = PropertyName;

            if (controlID.Length == 0)
            {
                throw new ArgumentException(SR.GetString(SR.ControlParameter_ControlIDNotSpecified, Name));
            }

            Control foundControl = DataBoundControlHelper.FindControl(control, controlID);

            if (foundControl == null)
            {
                throw new InvalidOperationException(SR.GetString(SR.ControlParameter_CouldNotFindControl, controlID, Name));
            }

            ControlValuePropertyAttribute controlValueProp = (ControlValuePropertyAttribute)TypeDescriptor.GetAttributes(foundControl)[typeof(ControlValuePropertyAttribute)];

            // If no property name is specified, use the ControlValuePropertyAttribute to determine which property to use.
            if (propertyName.Length == 0)
            {
                if ((controlValueProp != null) && (!String.IsNullOrEmpty(controlValueProp.Name)))
                {
                    propertyName = controlValueProp.Name;
                }
                else
                {
                    throw new InvalidOperationException(SR.GetString(SR.ControlParameter_PropertyNameNotSpecified, controlID, Name));
                }
            }

            // Get the value of the property
            object value = DataBinder.Eval(foundControl, propertyName);

            // Convert the value to null if this is the default property and the value is the property's default value
            if (controlValueProp != null &&
                String.Equals(controlValueProp.Name, propertyName, StringComparison.OrdinalIgnoreCase) &&
                controlValueProp.DefaultValue != null &&
                controlValueProp.DefaultValue.Equals(value))
            {
                return(null);
            }
            return(value);
        }
Example #3
0
        protected override object Evaluate(HttpContext ctx, Control control)
        {
            if (control == null)
            {
                return(null);
            }
            if (control.Page == null)
            {
                return(null);
            }

            if (String.IsNullOrEmpty(ControlID))
            {
                throw new ArgumentException("The ControlID property is not set.");
            }

            Control c = null, namingContainer = control.NamingContainer;

            while (namingContainer != null)
            {
                c = namingContainer.FindControl(ControlID);
                if (c != null)
                {
                    break;
                }
                namingContainer = namingContainer.NamingContainer;
            }
            if (c == null)
            {
                throw new InvalidOperationException("Control '" + ControlID + "' not found.");
            }

            string propName = PropertyName;

            if (String.IsNullOrEmpty(propName))
            {
                object [] attrs = c.GetType().GetCustomAttributes(typeof(ControlValuePropertyAttribute), true);
                if (attrs.Length == 0)
                {
                    throw new ArgumentException("The PropertyName property is not set and the Control identified by the ControlID property is not decorated with a ControlValuePropertyAttribute attribute.");
                }
                ControlValuePropertyAttribute attr = (ControlValuePropertyAttribute)attrs [0];
                propName = attr.Name;
            }

            return(DataBinder.Eval(c, propName));
        }
        protected override object FetchValue(string controlId)
        {
            if (String.IsNullOrEmpty(controlId))
            {
                return(null);
            }

            Control dataControl = ModelBindingExecutionContext.GetService <Control>();

            //Following code taken from ControlParameter - code duplicated because ControlPrameter throws exceptions whereas we do not.
            string propertyName = PropertyName;

            //Bug Fix # 280051 : First try to find it on dataControl as DataBoundControlHelper.FindControl only walks up starting from dataControl's NamingContainer.
            Control foundControl = dataControl.FindControl(controlId) ?? DataBoundControlHelper.FindControl(dataControl, controlId);

            if (foundControl == null)
            {
                return(null);
            }

            ControlValuePropertyAttribute controlValueProp = (ControlValuePropertyAttribute)TypeDescriptor.GetAttributes(foundControl)[typeof(ControlValuePropertyAttribute)];

            // If no property name is specified, use the ControlValuePropertyAttribute to determine which property to use.
            if (String.IsNullOrEmpty(propertyName))
            {
                if ((controlValueProp != null) && (!String.IsNullOrEmpty(controlValueProp.Name)))
                {
                    propertyName = controlValueProp.Name;
                }
                else
                {
                    return(null);
                }
            }

            // Get the value of the property
            object value = DataBinder.Eval(foundControl, propertyName);

            // Convert the value to null if this is the default property and the value is the property's default value
            if (controlValueProp != null &&
                controlValueProp.DefaultValue != null &&
                controlValueProp.DefaultValue.Equals(value))
            {
                return(null);
            }
            return(value);
        }
Example #5
0
        protected internal override object Evaluate(HttpContext context, Control control)
        {
            if (control == null)
            {
                return(null);
            }
            string controlID    = this.ControlID;
            string propertyName = this.PropertyName;

            if (controlID.Length == 0)
            {
                throw new ArgumentException(System.Web.SR.GetString("ControlParameter_ControlIDNotSpecified", new object[] { base.Name }));
            }
            Control component = DataBoundControlHelper.FindControl(control, controlID);

            if (component == null)
            {
                throw new InvalidOperationException(System.Web.SR.GetString("ControlParameter_CouldNotFindControl", new object[] { controlID, base.Name }));
            }
            ControlValuePropertyAttribute attribute = (ControlValuePropertyAttribute)TypeDescriptor.GetAttributes(component)[typeof(ControlValuePropertyAttribute)];

            if (propertyName.Length == 0)
            {
                if ((attribute == null) || string.IsNullOrEmpty(attribute.Name))
                {
                    throw new InvalidOperationException(System.Web.SR.GetString("ControlParameter_PropertyNameNotSpecified", new object[] { controlID, base.Name }));
                }
                propertyName = attribute.Name;
            }
            object obj2 = DataBinder.Eval(component, propertyName);

            if (((attribute != null) && string.Equals(attribute.Name, propertyName, StringComparison.OrdinalIgnoreCase)) && ((attribute.DefaultValue != null) && attribute.DefaultValue.Equals(obj2)))
            {
                return(null);
            }
            return(obj2);
        }