Beispiel #1
0
        private Object GetControlValue(Control control, Type type)
        {
            var controlType = control.GetType();

            var getter = UIPropertyMapping.GetGetter(controlType);

            if (getter != null)
            {
                return(getter(control, type));
            }

            if (control is ISingleValueContainer)
            {
                var container = (ISingleValueContainer)control;
                return(container.Value);
            }

            if (control is RadioButton)
            {
                var container = (RadioButton)control;
                return(container.Checked);
            }

            if (control is CheckBox)
            {
                var container = (CheckBox)control;
                return(container.Checked);
            }

            if (control is TextBox)
            {
                var container = (TextBox)control;
                return(container.Text);
            }

            if (control is Label)
            {
                var container = (Label)control;
                return(container.Text);
            }

            if (control is FileUpload)
            {
                var container = (FileUpload)control;
                return(container.FileBytes);
            }

            if (control is ASPxSpinEdit)
            {
                var container = (ASPxSpinEdit)control;
                return(container.Value);
            }

            if (control is ASPxDateEdit)
            {
                var container = (ASPxDateEdit)control;
                return(container.Value);
            }

            if (control is ASPxTimeEdit)
            {
                var container = (ASPxTimeEdit)control;
                return(container.Value);
            }

            if (control is ASPxHtmlEditor)
            {
                var container = (ASPxHtmlEditor)control;
                return(container.Html);
            }

            if (control is ListControl)
            {
                var container = (ListControl)control;
                var @set      = new HashSet <String>();

                foreach (ListItem item in container.Items)
                {
                    if (item.Selected)
                    {
                        @set.Add(item.Value);
                    }
                }

                if (type == typeof(String))
                {
                    foreach (var item in set)
                    {
                        return(item);
                    }

                    return(null);
                }

                return(@set);
            }

            if (control is ASPxComboBox)
            {
                var container = (ASPxComboBox)control;

                var selItem = container.SelectedItem;
                if (selItem != null)
                {
                    return(selItem.Value);
                }

                if (container.DropDownStyle == DropDownStyle.DropDown)
                {
                    return(container.Text);
                }

                return(null);
            }


            throw new Exception("Unable to detect control value");
        }
Beispiel #2
0
        private Object ConvertValue(Object value, Type type)
        {
            var converterFunc = UIPropertyMapping.GetConverter(type);

            if (converterFunc != null)
            {
                return(converterFunc(value, type));
            }

            if (type.IsInstanceOfType(value))
            {
                return(value);
            }

            var strValue = Convert.ToString(value, CultureInfo.CurrentUICulture);

            if (String.IsNullOrEmpty(strValue))
            {
                if (!type.IsValueType || ReflectionUtil.IsNullable(type))
                {
                    return(null);
                }

                if (_allowDefaultIfNull)
                {
                    return(Activator.CreateInstance(type));
                }

                var nullValueErrorText = String.Format("Null is not assignable to type [{0}]", type);
                throw new Exception(nullValueErrorText);
            }

            var sourceType = value.GetType();

            if (ReflectionUtil.IsNullable(sourceType))
            {
                sourceType = Nullable.GetUnderlyingType(sourceType);
            }

            var destinationType = type;

            if (ReflectionUtil.IsNullable(destinationType))
            {
                destinationType = Nullable.GetUnderlyingType(destinationType);
            }

            var converter = TypeDescriptor.GetConverter(destinationType);

            if (converter.CanConvertFrom(sourceType))
            {
                return(converter.ConvertFrom(value));
            }

            converter = TypeDescriptor.GetConverter(sourceType);
            if (converter.CanConvertTo(destinationType))
            {
                return(converter.ConvertTo(value, destinationType));
            }

            var unableConvertErrorText = String.Format("Unable to convert value [{0} - {1}] to type [{2}]", value, value.GetType(), type);

            throw new Exception(unableConvertErrorText);
        }
Beispiel #3
0
        private void ControlPropertyValueSetter(Control control, Object propertyValue)
        {
            var controlType = control.GetType();

            var setter = UIPropertyMapping.GetSetter(controlType);

            if (setter != null)
            {
                setter(control, _propertyType, propertyValue);
                return;
            }

            if (control is ISingleValueContainer)
            {
                var container = (ISingleValueContainer)control;
                container.Value = propertyValue;

                return;
            }

            if (control is RadioButton)
            {
                var container = (RadioButton)control;
                var @bool     = (bool?)propertyValue;

                container.Checked = @bool.GetValueOrDefault();

                return;
            }

            if (control is CheckBox)
            {
                var container = (CheckBox)control;
                var @bool     = (bool?)propertyValue;

                container.Checked = @bool.GetValueOrDefault();

                return;
            }

            if (control is TextBox)
            {
                var container = (TextBox)control;
                container.Text = Convert.ToString(propertyValue);

                return;
            }

            if (control is Label)
            {
                var container = (Label)control;
                container.Text = Convert.ToString(propertyValue);

                return;
            }

            if (control is FileUpload)
            {
                var container = (FileUpload)control;
                //container.FileBytes = Convert.ToString(propertyValue);

                return;
            }

            if (control is ASPxSpinEdit)
            {
                var container = (ASPxSpinEdit)control;
                container.Value = propertyValue;

                return;
            }

            if (control is ASPxDateEdit)
            {
                var container = (ASPxDateEdit)control;
                container.Value = propertyValue;

                return;
            }

            if (control is ASPxTimeEdit)
            {
                var container = (ASPxTimeEdit)control;
                container.Value = propertyValue;

                return;
            }

            if (control is ASPxHtmlEditor)
            {
                var container = (ASPxHtmlEditor)control;
                container.Html = Convert.ToString(propertyValue);

                return;
            }

            if (control is ASPxComboBox)
            {
                var container = (ASPxComboBox)control;

                var selItem = container.Items.FindByValue(propertyValue);
                if (selItem != null)
                {
                    container.SelectedItem = selItem;
                }
                else if (container.DropDownStyle == DropDownStyle.DropDown)
                {
                    container.Text  = Convert.ToString(propertyValue);
                    container.Value = Convert.ToString(propertyValue);
                }

                return;
            }

            if (control is ListControl)
            {
                var container = (ListControl)control;

                var collection = propertyValue as IEnumerable <String>;
                if (collection != null)
                {
                    var @set = collection as ISet <String>;
                    @set = (@set ?? new HashSet <String>(collection));

                    foreach (ListItem item in container.Items)
                    {
                        item.Selected = @set.Contains(item.Value);
                    }
                }
                else
                {
                    foreach (ListItem item in container.Items)
                    {
                        item.Selected = (item.Value == Convert.ToString(propertyValue));
                    }
                }

                return;
            }

            throw new Exception("Unable to detect control type");
        }