ext.net 数据和表单值的交互(直接上封装的代码,看注释,不多解释)

public class ModelHander<T>
{
    /// <summary>
    /// 通过表单动态的填充实体类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="t"></param>
    /// <param name="form"></param>
    /// <returns></returns>
    public static int SetModelToForm(ref T t, NameValueCollection form, Page page)
    {
        int va = 0;
        Type type = t.GetType();//获取类型    
        PropertyInfo[] pi = type.GetProperties();//获取属性集合    
        Type fieldType;
        string ControlType;
        foreach (PropertyInfo p in pi)
        {
            //if (form[p.Name] != null && form[p.Name] != "") //影响RadioGroup
            {
                try
                {
                    fieldType = p.PropertyType;
                    ControlType = page.FindControl(p.Name).GetType().Name;
                    if (fieldType != null && fieldType.Name == "Nullable`1")//判断是否为可空类型
                    {
                        fieldType = Nullable.GetUnderlyingType(p.PropertyType);
                    }

                    if (ControlType == "ComboBox") //ComBox单独处理获取Value值
                    {
                        p.SetValue(t, Convert.ChangeType(((ComboBox)page.FindControl(p.Name)).SelectedItem.Value, fieldType), null);
                    }
                    else if (ControlType == "RadioGroup")
                    {
                        RadioGroup radioGroup = page.FindControl(p.Name) as RadioGroup;//此处没有被执行 待解决
                        for (int i = 0; i < radioGroup.Items.Count; i++)
                        {
                            if (radioGroup.Items[i].Checked == true)
                            {
                                p.SetValue(t, Convert.ChangeType(radioGroup.Items[i].InputValue, fieldType), null);
                                break;
                            }
                        }
                    }
                    else if (ControlType == "Checkbox")
                    {
                        if (((Checkbox)page.FindControl(p.Name)).Checked)
                        {
                            p.SetValue(t, Convert.ChangeType("1", fieldType), null);
                        }
                        else
                        {
                            p.SetValue(t, Convert.ChangeType("0", fieldType), null);
                        }
                    }
                    else if (ControlType == "Label") //ComBox单独处理获取Value值
                    {
                        p.SetValue(t, Convert.ChangeType(((Label)page.FindControl(p.Name)).Text, fieldType), null);
                    }
                    else if (ControlType == "HtmlEditor") //ComBox单独处理获取Value值
                    {
                        p.SetValue(t, Convert.ChangeType(((HtmlEditor)page.FindControl(p.Name)).Text, fieldType), null);
                    }
                    else if (ControlType == "DateField") //DateField单独处理获取Value值
                    {
                        p.SetValue(t, Convert.ChangeType(((DateField)page.FindControl(p.Name)).Text, fieldType), null);
                    }
                    else
                    {
                        p.SetValue(t, Convert.ChangeType(form[p.Name], fieldType), null);//为属性赋值,并转换键值的类型为该属性的类型
                    }
                    va++;//记录赋值成功的属性数    
                }
                catch
                {
                }
            }
        }
        return va;
    }

    /// <summary>
    /// 设置控件值
    /// Control:TextField,TextArea,Label,DateField,MultiCombo,ComboBox,Radio,Checkbox,NumberField
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="t"></param>
    /// <param name="page">当前页面,从中寻找控件</param>
    /// <param name="actType">动作类型(EDIT、QUERY)</param>
    public static void SetDataOfControl(T t, Page page, string actType)
    {
        if (t==null)
        {
            return;
        }
        Type type = t.GetType();
        PropertyInfo[] pi = type.GetProperties();
        string ControlType;
        object obj;
        //string[] MultiItems;
        actType = actType.ToUpper();//动作类型
        foreach (PropertyInfo p in pi)
        {
            if (p.Name != "ID")
            {
                try
                {
                    obj = p.GetValue(t, null);
                    ControlType = page.FindControl(p.Name).GetType().Name;

                    switch (ControlType)
                    {
                        case "TextField":
                            TextField textField = ((TextField)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                textField.SetValue(obj);
                            }
                            if (actType == "QUERY")//查询时控件置灰,只读
                            {
                                //textField.Enabled = false;
                                textField.ReadOnly = true;
                                //textField.Cls = "textDisabled";
                                //textField.Disabled = true;
                            }
                            break;
                        case "TextArea":
                            TextArea textArea = ((TextArea)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                textArea.Text = obj.ToString();
                            }
                            if (actType == "QUERY")
                            {
                                //textArea.Enabled = false;
                                textArea.ReadOnly = true;
                            }
                            break;
                        case "Label":
                            Label label = ((Label)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                label.Text = obj.ToString();
                            }
                            break;
                        case "DateField":
                            DateField dateField = ((DateField)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                dateField.Text = obj.ToString();
                            }
                            if (actType == "QUERY")//查询时控件置灰,只读
                            {
                                //dateField.Enabled = false;
                                dateField.ReadOnly = true;
                            }
                            break;
                        case "MultiCombo":
                            MultiCombo multiCombo = ((MultiCombo)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                //MultiItems=obj.ToString().Split(',');
                                //foreach (string item in MultiItems)
                                //{
                                //    multiCombo.SelectItem(item);
                                multiCombo.Value = obj.ToString();
                                //}
                            }
                            if (actType == "QUERY")
                            {
                                multiCombo.ReadOnly = true;
                                //multiCombo.Disabled = true;
                            }
                            break;
                        case "ComboBox":
                            ComboBox comboBox = ((ComboBox)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                comboBox.SelectedItem.Value = obj.ToString();
                            }
                            if (actType == "QUERY")
                            {
                                comboBox.ReadOnly = true;
                                //comboBox.Disabled = true;
                            }
                            break;
                        case "RadioGroup":
                            RadioGroup radioGroup = page.FindControl(p.Name) as RadioGroup;
                            for (int i = 0; i < radioGroup.Items.Count; i++)
                            {
                                if (obj != null && radioGroup.Items[i].InputValue == obj.ToString())
                                {
                                    radioGroup.Items[i].Checked = true;
                                }
                            }
                            if (actType == "QUERY")
                            {
                                //radioGroup.Enabled = false;
                                //radioGroup.ReadOnly = true;
                                radioGroup.Disabled = true;
                            }
                            break;
                        case "Checkbox":
                            Checkbox checkbox = ((Checkbox)page.FindControl(p.Name));

                            if (obj != null && obj.ToString() == "1")
                            {
                                checkbox.Checked = true;
                            }
                            else
                            {
                                checkbox.Checked = false;
                            }

                            if (actType == "QUERY")
                            {
                                checkbox.Enabled = false;
                                //checkbox.ReadOnly = true;
                            }
                            break;
                        case "Hidden":
                            Hidden hidden = ((Hidden)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                hidden.Text = obj.ToString();
                            }
                            if (actType == "QUERY")//查询时控件置灰,只读
                            {
                                //textField.Enabled = false;
                                hidden.ReadOnly = true;
                                //textField.Cls = "textDisabled";
                                //textField.Disabled = true;
                            }
                            break;
                        case "NumberField":
                            NumberField numberField = ((NumberField)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                numberField.Text = obj.ToString();
                            }
                            if (actType == "QUERY")//查询时控件置灰,只读
                            {
                                //textField.Enabled = false;
                                numberField.ReadOnly = true;
                            }
                            break;
                        case "TriggerField":
                            TriggerField triggerField = ((TriggerField)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                triggerField.Text = obj.ToString();
                            }
                            if (actType == "QUERY")//查询时控件置灰,只读
                            {
                                //textField.Enabled = false;
                                triggerField.ReadOnly = true;
                            }
                            break;
                        case "HtmlEditor":
                            HtmlEditor HtmlEditor = ((HtmlEditor)page.FindControl(p.Name));
                            if (obj != null)
                            {
                                HtmlEditor.Text = obj.ToString();
                            }
                            if (actType == "QUERY")//查询时控件置灰,只读
                            {
                                //textField.Enabled = false;
                                HtmlEditor.ReadOnly = true;
                                //textField.Cls = "textDisabled";
                                //textField.Disabled = true;
                            }
                            break;
                        //case "GridPanel":
                        //    GridPanel gridPanel = ((GridPanel)page.FindControl(p.Name));
                        //    //if (obj != null)
                        //    //{
                        //    //    gridPanel.Text = obj.ToString();
                        //    //}
                        //    if (actType == "QUERY")//查询时控件置灰,只读
                        //    {
                        //        GridPanelHelper.SetColumnsEditable(gridPanel.ColumnModel, false);
                        //    }
                        //    else
                        //    {
                        //        GridPanelHelper.SetColumnsEditable(gridPanel.ColumnModel, true);
                        //    }
                        //    break;
                        default:
                            break;
                    }
                }
                catch
                {
                }
            }
        }//end foreach
        //置灰设置人员按钮
        if (page.FindControl("AskManBtn") != null && actType == "QUERY")
        {
            Button button = page.FindControl("AskManBtn") as Button;
            button.Disabled = true;
        }
        if (page.FindControl("gridAppFile") != null && actType == "QUERY")
        {
            FileUploadField fileUpload = page.FindControl("f2") as FileUploadField;
            fileUpload.Disabled = true;

            GridPanel gridPanel = page.FindControl("gridAppFile") as GridPanel;
            gridPanel.ColumnModel.Columns[2].Hidden = true;
        }
        if (page.FindControl("BtnAdd") != null && actType == "QUERY")
        {
            Button btn = page.FindControl("BtnAdd") as Button;
            btn.Disabled = true;
        }
        if (page.FindControl("BtnDel") != null && actType == "QUERY")
        {
            Button btn = page.FindControl("BtnDel") as Button;
            btn.Disabled = true;
        }
    }
    public static DataTable GetUserByID(int userid)
    {
        string sql = "select UName,g_group.GName as UClass,UWork  from u_user left join ug_User_Group " +
                    " on u_user.UId=ug_User_Group.uId " +
                    " left join g_group on g_group.GId=ug_User_Group.gId " +
                    " where u_user.UId='" + userid + "'";
        DataTable dt = Sunway.DBUtility.DbHelperSQL.ExecuteDataSet(CommandType.Text, sql, null).Tables[0];
        return dt;
    }

    /// <summary>
    /// 创建GridPanel的列及Store
    /// </summary>
    public static void CreateGrdStore(T t)
    {
        Type type = t.GetType();
        PropertyInfo[] pi = type.GetProperties();
        string ControlType;
    }
}
给对应页面控件进行赋值

   ModelHander<db_BackApply>.SetDataOfControl(model, this.Page, "QUERY");
获得其mode值

   ModelHander<db_BackApply>.SetModelToForm(ref model, Request.Form, this.Page);



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。