Esempio n. 1
0
        public static FieldControl CreateDefaultFieldControl(Field field)
        {
            FieldControl control = null;

            var choiceSetting = field.FieldSetting as ChoiceFieldSetting;
            var longTextSetting = field.FieldSetting as LongTextFieldSetting;
            var dateTimeFieldSetting = field.FieldSetting as DateTimeFieldSetting;
            string hint = field.FieldSetting.ControlHint;

            if (!string.IsNullOrEmpty(hint))
                control = CreateFieldControlByHint(hint);
            //choice field?
            else if (choiceSetting != null)
                control = CreateDefaultChoiceControl(choiceSetting);
            //longtext field?
            else if (longTextSetting != null)
                control = CreateDefaultLongTextControl(longTextSetting);
            //datetime field?
            else if (dateTimeFieldSetting != null)
                control = CreateDefaultDateTimeControl(dateTimeFieldSetting);

            //generic way, also a fallback logic if we don't have a field control by now
            if (control == null)
            {
                Type controlType = null;

                if (!ControlTable.TryGetValue(field.FieldSetting.ShortName, out controlType))
                    throw new ApplicationException(String.Concat("Cannot resolve the generic field control by '", field.GetType().FullName, "'"));

                control = (FieldControl)Activator.CreateInstance(controlType);
            }

            if (control == null)
                throw new ApplicationException(string.Concat("Failed to instantiate a field control for field ", field.Name));

            control.FieldName = field.Name;
            control.DoAutoConfigure(field.FieldSetting);

            return control;
        }