Ejemplo n.º 1
0
        public static FormControlBase GetFormControl(Column field)
        {
            FormControlBase result = null;

            switch (field.Type.ToLower())
            {
            case "datetime":
                result = new DatePicker();
                break;

            case "bit":
                result = new DropDownList()
                {
                    DataSource = GetDataSource(field) as IKeyValueDataSource
                };
                break;

            default:
                result = new TextBox();
                break;
            }
            result.Id       = field.Name;
            result.Name     = field.Name;
            result.Label    = field.Comment;
            result.Rank     = field.Rank;
            result.Enable   = true;
            result.Visiable = true;
            return(result);
        }
Ejemplo n.º 2
0
        public static Form GetFormComponent(Type entityType)
        {
            string formId = NamingCenter.GetEntityFormId(entityType);
            string key    = NamingCenter.GetCacheKey(CacheType.ENTITY_CONFIG, formId);
            var    result = CacheHelper.GetFromCache <Form>(key, () =>
            {
                var returnValue = new Form();
                returnValue.Id  = formId;
                var pis         = FastType.Get(entityType).Setters;
                int rank        = 0;
                foreach (var fp in pis)
                {
                    var p = fp.Info;
                    if (!(p.PropertyType == typeof(string)) && !p.PropertyType.IsValueType)
                    {
                        continue;
                    }
                    object defaultValue            = null;
                    string label                   = string.Empty;
                    FormControlBase fieldComponent = null;
                    bool canModify                 = true;
                    var formAttr                   = p.GetCustomAttribute <FormFieldAttribute>(true);
                    if (formAttr != null)
                    {
                        if (!formAttr.Editable)
                        {
                            continue;
                        }
                        label        = formAttr.FormDisplayName;
                        defaultValue = formAttr.DefaultValue;
                        if (formAttr.ControlType != ControlType.None)
                        {
                            switch (formAttr.ControlType)
                            {
                            case ControlType.DateTimeRange:
                            case ControlType.DatePicker:
                                fieldComponent = new DatePicker();
                                break;

                            case ControlType.DropDownList:
                                fieldComponent = new DropDownList();
                                break;

                            case ControlType.Password:
                                fieldComponent = new TextBox()
                                {
                                    TextMode = TextMode.Password
                                };
                                break;

                            case ControlType.CheckBox:
                                fieldComponent = new CheckBox();
                                break;

                            case ControlType.CheckBoxList:
                                fieldComponent = new CheckBoxList();
                                break;

                            case ControlType.RadioButton:
                                fieldComponent = new RadioButton();
                                break;

                            case ControlType.RadioButtonList:
                                fieldComponent = new RadioButtonList();
                                break;

                            case ControlType.TextArea:
                                fieldComponent = new TextArea();
                                break;

                            case ControlType.TextEditor:
                                fieldComponent = new TextEditor();
                                break;
                            }
                        }
                        canModify = formAttr.CanModity;
                    }
                    if (fieldComponent == null)
                    {
                        if (p.PropertyType.IsEnum)
                        {
                            fieldComponent = new DropDownList()
                            {
                                DataSource = new EnumDataSource()
                                {
                                    EnumTypeFullName = p.PropertyType.FullName, EnumValueType = EnumValueType.Code
                                }
                            };
                        }
                        else if (p.PropertyType == typeof(bool))
                        {
                            fieldComponent = new DropDownList()
                            {
                                DataSource = new EnumDataSource()
                                {
                                    EnumTypeFullName = typeof(YesOrNo).FullName, EnumValueType = EnumValueType.Code
                                }
                            };
                        }
                        else if (p.PropertyType == typeof(DateTime))
                        {
                            fieldComponent = new DatePicker();
                        }
                        else
                        {
                            fieldComponent = new TextBox();
                        }
                    }
                    var formAttr1 = p.GetCustomAttribute <DataSourceAttribute>(true);
                    if (formAttr1 != null)
                    {
                        if (fieldComponent is IKeyValueDataSourceControl)
                        {
                            (fieldComponent as IKeyValueDataSourceControl).DataSource = formAttr1.GetDataSource() as IKeyValueDataSource;
                        }
                    }
                    fieldComponent.Description = p.GetDescription();
                    if (label.IsNullOrEmpty())
                    {
                        label = p.GetDisplayName();
                    }
                    var attr1 = p.GetCustomAttribute <DefaultValueAttribute>(true);
                    if (attr1 != null)
                    {
                        defaultValue = attr1.Value;
                    }
                    var attr2 = p.GetCustomAttribute <ValidatorAttribute>(true);
                    if (attr2 != null)
                    {
                        fieldComponent.Validator = attr2.ValidateString;
                    }
                    fieldComponent.Label     = label;
                    fieldComponent.Id        = p.Name;
                    fieldComponent.CanModity = canModify;
                    fieldComponent.Rank      = (++rank);
                    fieldComponent.Value     = defaultValue == null ? "" : defaultValue.ToString();
                    returnValue.Fields.Add(fieldComponent);
                }
                foreach (var btn in ControlDefaultSetting.GetDefaultFormButton())
                {
                    returnValue.Buttons.Add(btn);
                }
                returnValue.Fields.Add(new HiddenField()
                {
                    Id = ControlModelBinder.EntityFullNameHiddenName, Value = entityType.FullName + "," + entityType.Assembly.FullName
                });
                return(returnValue);
            });

            return(result.Clone() as Form);
        }