private static Boolean GetControlValue(Control control, out Object value) { TypeX tx = control.GetType(); String name = tx.GetCustomAttributeValue <ControlValuePropertyAttribute, String>(); PropertyInfoX pix = null; if (!String.IsNullOrEmpty(name)) { pix = PropertyInfoX.Create(tx.Type, name); } if (pix == null) { pix = PropertyInfoX.Create(tx.Type, "Value"); } if (pix == null) { pix = PropertyInfoX.Create(tx.Type, "Text"); } if (pix != null) { value = pix.GetValue(control); return(true); } value = null; return(false); }
static String GetCustomAttributeValue <TAttribute>(MemberInfo member, String name) { TAttribute att = AttributeX.GetCustomAttribute <TAttribute>(member, true); if (att == null) { return(null); } PropertyInfoX pix = PropertyInfoX.Create(typeof(TAttribute), name); if (pix == null) { return(null); } return((String)pix.GetValue(att)); }
/// <summary> /// 将对象赋值 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="formModel">表单获取的实体对象</param> /// <param name="model">从数据库查询的实体对象</param> /// <returns></returns> public static T ConvertToModel <T>(T formModel, T model) { Type formModelType = formModel.GetType(); Type modelType = model.GetType(); PropertyInfo[] props = modelType.GetProperties(); //获取此对象的,字段集合 PropertyInfo[] formProps = formModelType.GetProperties(); //获取此对象的,字段集合 object propertValue = null; string value = String.Empty; for (int i = 0; i < props.Length; i++) { if (props[i].Name.Equals(formProps[i].Name) && props[i].CanWrite == true) { propertValue = PropertyInfoX.GetValue(formModel, formProps[i].Name); if (propertValue != null) { PropertyInfoX.SetValue(model, props[i].Name, propertValue);//给字段赋值 } } } return(model); }
/// <summary> /// 把实体成员的值设置到控件上 /// </summary> /// <param name="field"></param> /// <param name="control"></param> /// <param name="canSave"></param> protected virtual void SetFormItem(FieldItem field, System.Web.UI.Control control, Boolean canSave) { if (field == null || control == null) { return; } String toolTip = String.IsNullOrEmpty(field.Description) ? field.Name : field.Description; if (field.IsNullable) { toolTip = String.Format("请填写{0}!", toolTip); } else { toolTip = String.Format("必须填写{0}!", toolTip); } if (control is Label) { toolTip = null; } if (control is ExtAspNet.ControlBase) { ExtAspNet.ControlBase wc = control as ExtAspNet.ControlBase; // 设置ToolTip //if (String.IsNullOrEmpty(wc.ToolTip) && !String.IsNullOrEmpty(toolTip)) wc.ToolTip = toolTip; //// 必填项 //if (!field.IsNullable) SetNotAllowNull(field, control, canSave); // 设置只读,只有不能保存时才设置,因为一般都不是只读,而前端可能自己设置了控件为只读,这种情况下这里再设置就会修改前端的意思 if (!canSave) { if (wc is ExtAspNet.TextBox) { (wc as TextBox).Enabled = !canSave; } else { wc.Enabled = canSave; } } // 分控件处理 if (wc is TextBox || wc is DatePicker) { SetFormItemTextBox(field, wc as RealTextField, canSave); } else if (wc is Label) { SetFormItemLabel(field, wc as Label, canSave); } else if (wc is RadioButton) { SetFormItemRadioButton(field, wc as RadioButton, canSave); } else if (wc is CheckBox) { SetFormItemCheckBox(field, wc as CheckBox, canSave); } else if (wc is DropDownList) { SetFormItemListControl(field, wc as DropDownList, canSave); } else { SetControlValue(control as ControlBase, Entity[field.Name]); } } else { SetControlValue(control as ControlBase, Entity[field.Name]); PropertyInfoX pix = PropertyInfoX.Create(control.GetType(), "ToolTip"); if (pix != null && String.IsNullOrEmpty((String)pix.GetValue(control))) { pix.SetValue(control, toolTip); } } }