Exemplo n.º 1
0
        /// <summary>
        /// 将控件的值赋给model
        /// </summary>
        /// <param name="obj">model</param>
        /// <param name="container">control</param>
        /// <returns></returns>
        public static object GetValueToObject(object obj, Control container)
        {
            if (obj == null)
            {
                return(null);
            }

            Type objType = obj.GetType();

            PropertyInfo[] objPropertiesArray = objType.GetProperties();

            //遍历属性集合
            foreach (PropertyInfo objProperty in objPropertiesArray)
            {
                Control control = container.FindControl(objProperty.Name);

                if (control != null)
                {
                    //取当前属性项的字段类型
                    Type ModelType = objProperty.PropertyType;

                    if (control is TextBox)
                    {
                        TextBox txt = (TextBox)control;

                        object objvalue = txt.Text == "" ? null : SetObjValueByType(ModelType, txt.Text);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is HiddenField)
                    {
                        HiddenField txt      = (HiddenField)control;
                        object      objvalue = txt.Value == "" ? null : SetObjValueByType(ModelType, txt.Value);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DropDownList)
                    {
                        DropDownList drp = (DropDownList)control;

                        object objvalue = SetObjValueByType(ModelType, drp.Text);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is CheckBox)
                    {
                        CheckBox chk = (CheckBox)control;

                        object objvalue = SetObjValueByType(ModelType, chk.Checked);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlTextBoxEx)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlTextBoxEx txt = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlTextBoxEx)control;

                        //object objvalue = txt.Text == "" ? null : SetObjValueByType(ModelType, txt.Text);
                        object objvalue = SetObjValueByType(ModelType, txt.Text);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownArea)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownArea drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownArea)control;

                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownReportType)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownReportType drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownReportType)control;

                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is ctrlDropDownTrainPlan)
                    {
                        ctrlDropDownTrainPlan plan = control as ctrlDropDownTrainPlan;
                        object objvalue            = SetObjValueByType(ModelType, plan.SelectedValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is ctrDropDownCellTmp)
                    {
                        ctrDropDownCellTmp drp      = control as ctrDropDownCellTmp;
                        object             objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownModule)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownModule drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownModule)control;

                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownFileListTmp)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownFileListTmp drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownFileListTmp)control;

                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCompanyInfo)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCompanyInfo drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCompanyInfo)control;

                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystemInfo)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystemInfo drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystemInfo)control;

                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlArchiveFormType)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlArchiveFormType drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlArchiveFormType)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is CommonCtrl.ctrlOptStatus)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlOptStatus drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlOptStatus)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownRole)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownRole drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownRole)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCanDefineType)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCanDefineType drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCanDefineType)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCutLeng)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCutLeng drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCutLeng)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSplit)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSplit drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSplit)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystem)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystem drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystem)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is RadioButtonList)
                    {
                        ListControl rl = (ListControl)control;

                        object objvalue = SetObjValueByType(ModelType, rl.SelectedValue);

                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is CommonCtrl.ctrlDropDownConstructionProject)
                    {
                        CommonCtrl.ctrlDropDownConstructionProject drp = (CommonCtrl.ctrlDropDownConstructionProject)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is CommonCtrl.ctrlDropDownUser)
                    {
                        CommonCtrl.ctrlDropDownUser drp = (CommonCtrl.ctrlDropDownUser)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is CommonCtrl.ctrlDropDownWorkFlow)
                    {
                        CommonCtrl.ctrlDropDownWorkFlow drp = (CommonCtrl.ctrlDropDownWorkFlow)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is CommonCtrl.ctrlDropDownSingleProject)
                    {
                        CommonCtrl.ctrlDropDownSingleProject drp = (CommonCtrl.ctrlDropDownSingleProject)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is CommonCtrl.ctrlDropDownSingleProjectUsers)
                    {
                        CommonCtrl.ctrlDropDownSingleProjectUsers drp = (CommonCtrl.ctrlDropDownSingleProjectUsers)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                    else if (control is CommonCtrl.ctrlDropDownFileList)
                    {
                        CommonCtrl.ctrlDropDownFileList drp = (CommonCtrl.ctrlDropDownFileList)control;
                        object objvalue = SetObjValueByType(ModelType, drp.SelectValue);
                        if (objvalue != null)
                        {
                            objProperty.SetValue(obj, objvalue, null);
                        }
                    }
                }
            }

            return(obj);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 将model的值赋给控件
        /// </summary>
        /// <param name="obj">model</param>
        /// <param name="container">control</param>
        /// <returns></returns>
        public static object SetValueToPage(object obj, Control container)
        {
            if (obj == null)
            {
                return(null);
            }

            Type objType = obj.GetType();

            PropertyInfo[] objPropertiesArray = objType.GetProperties();

            foreach (PropertyInfo objProperty in objPropertiesArray)
            {
                Control control = container.FindControl(objProperty.Name);

                if (control != null)
                {
                    if (objProperty.GetValue(obj, null) == null)
                    {
                        continue;
                    }
                    object ctlValue = objProperty.GetValue(obj, null).ToString();

                    if (control is TextBox)
                    {
                        TextBox txt = (TextBox)control;

                        txt.Text = ctlValue.ToString();
                    }
                    else if (control is Label)
                    {
                        Label txt = (Label)control;

                        txt.Text = ctlValue.ToString();
                    }
                    else if (control is Literal)
                    {
                        Literal txt = (Literal)control;

                        txt.Text = ctlValue.ToString();
                    }
                    else if (control is HiddenField)
                    {
                        HiddenField txt = (HiddenField)control;
                        txt.Value = ctlValue.ToString();
                    }
                    else if (control is DropDownList)
                    {
                        DropDownList drp = (DropDownList)control;

                        drp.Text = ctlValue.ToString();
                    }
                    else if (control is CheckBox)
                    {
                        CheckBox chk = (CheckBox)control;

                        if (ctlValue != null && ctlValue.ToString().Trim() == "1")
                        {
                            chk.Checked = true;
                        }
                        else if (ctlValue != null && ctlValue.ToString().Trim() == "0")
                        {
                            chk.Checked = false;
                        }
                        else
                        {
                            chk.Checked = Convert.ToBoolean(ctlValue);
                        }
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlTextBoxEx)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlTextBoxEx txt = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlTextBoxEx)control;

                        txt.Text = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownArea)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownArea drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownArea)control;

                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownReportType)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownReportType drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownReportType)control;

                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownModule)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownModule drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownModule)control;

                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownFileListTmp)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownFileListTmp drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownFileListTmp)control;

                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCompanyInfo)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCompanyInfo drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCompanyInfo)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystem)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystem drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystem)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystemInfo)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystemInfo drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSystemInfo)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlArchiveFormType)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlArchiveFormType drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlArchiveFormType)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is CommonCtrl.ctrlOptStatus)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlOptStatus drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlOptStatus)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCanDefineType)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCanDefineType drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCanDefineType)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCutLeng)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCutLeng drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownCutLeng)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSplit)
                    {
                        DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSplit drp = (DigiPower.Onlinecol.Standard.Web.CommonCtrl.ctrlDropDownSplit)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is RadioButtonList)
                    {
                        ListControl rl = (ListControl)control;

                        rl.SelectedValue = ctlValue.ToString();
                    }
                    else if (control is CommonCtrl.ctrlDropDownConstructionProject)
                    {
                        CommonCtrl.ctrlDropDownConstructionProject drp = (CommonCtrl.ctrlDropDownConstructionProject)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is CommonCtrl.ctrlDropDownUser)
                    {
                        CommonCtrl.ctrlDropDownUser drp = (CommonCtrl.ctrlDropDownUser)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is CommonCtrl.ctrlDropDownWorkFlow)
                    {
                        CommonCtrl.ctrlDropDownWorkFlow drp = (CommonCtrl.ctrlDropDownWorkFlow)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is CommonCtrl.ctrlDropDownFileList)
                    {
                        CommonCtrl.ctrlDropDownFileList drp = (CommonCtrl.ctrlDropDownFileList)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is CommonCtrl.ctrlDropDownSingleProject)
                    {
                        CommonCtrl.ctrlDropDownSingleProject drp = (CommonCtrl.ctrlDropDownSingleProject)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is CommonCtrl.ctrlDropDownSingleProjectUsers)
                    {
                        CommonCtrl.ctrlDropDownSingleProjectUsers drp = (CommonCtrl.ctrlDropDownSingleProjectUsers)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                    else if (control is CommonCtrl.ctrlDropDownRole)
                    {
                        CommonCtrl.ctrlDropDownRole drp = (CommonCtrl.ctrlDropDownRole)control;
                        drp.SelectValue = ctlValue.ToString();
                    }
                }
            }

            return(obj);
        }