玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

FormView Insert&Update反射取值賦給對象

Posted on 2008-12-08 17:41 玄铁剑 阅读(803) 评论(0)  编辑 收藏 引用 所属分类: asp.netC# Winform
static public DataTable ToDataTable<T>(List<T> entitys)
{
        if (entitys == null || entitys.Count < 1) 
        { 
            return null; 
        } 

        DataTable dt = new DataTable(); 
        Type entityType = entitys[0].GetType(); 
        PropertyInfo[] entityProperties = entityType.GetProperties(); 

        for (int i = 0; i < entityProperties.Length; i++) 
        { 
                dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType); 
        } 

        foreach (object entity in entitys) 
        { 
                if (entity.GetType() != entityType) 
                { 
                        continue;//Skip,the Datetype is not identity 
                } 
                object[] entityValues = new object[entityProperties.Length]; 
                for (int i = 0; i < entityProperties.Length; i++) 
                { 
                        entityValues[i] = entityProperties[i].GetValue(entity, null); 

                } 
                dt.Rows.Add(entityValues); 
        } 
        return dt;
}

static public void WriteFormViewInsertValue(object ObjEntity, FormViewInsertEventArgs e)

        OverWriteFormViewValue(ObjEntity, e.Values);
}

static public void WriteFormViewDeleteValue(object ObjEntity, FormViewDeleteEventArgs e)

        OverWriteFormViewValue(ObjEntity, e.Values);
}

static public void WriteFormViewUpdateOldValue(object ObjEntity, FormViewUpdateEventArgs e)

        OverWriteFormViewValue(ObjEntity, e.OldValues);
}

static public void WriteFormViewUpdateNewValue(object ObjEntity, FormViewUpdateEventArgs e)

        OverWriteFormViewValue(ObjEntity, e.NewValues);
}

static private void OverWriteFormViewValue(object ObjEntity, IOrderedDictionary DictValueList)

        foreach (DictionaryEntry entry in DictValueList) 
        { 
                Type ObjValue = ObjEntity.GetType(); 
                if (ObjValue.GetProperty(entry.Key.ToString()) != null) 
                { 
                        DictValueList[entry.Key.ToString()] = ObjValue.GetProperty(entry.Key.ToString()).GetValue(ObjEntity, null); 
                } 
        }
}

static private void ParseFormViewValue(object ObjEntity, IOrderedDictionary DictValueList)

        foreach (DictionaryEntry entry in DictValueList) 
        { 
                Type ObjValue = ObjEntity.GetType(); 
                if (ObjValue.GetProperty(entry.Key.ToString()) != null) 
                { 
                        string StrDateType = ObjValue.GetProperty(entry.Key.ToString()).PropertyType.ToString(); 
                        Boolean bNullable = StrDateType.Contains("System.Nullable`1"); 
                        if (bNullable == true) 
                                StrDateType = StrDateType.Replace("System.Nullable`1", "").Replace("[", "").Replace("]", ""); 
                        try 
                        { 
                                switch (StrDateType) 
                                { 
                                        case "System.DateTime": 
                                                ObjValue.GetProperty(entry.Key.ToString()).SetValue(ObjEntity, DateTime.Parse(entry.Value.ToString()), null); 
                                        break; 
                                        case "System.Boolean": 
                                                ObjValue.GetProperty(entry.Key.ToString()).SetValue(ObjEntity, Boolean.Parse(entry.Value.ToString()), null); 
                                        break; 
                                        case "System.Decimal": 
                                                ObjValue.GetProperty(entry.Key.ToString()).SetValue(ObjEntity, Decimal.Parse(entry.Value.ToString()), null); 
                                        break; 
                                        case "System.Double": 
                                                ObjValue.GetProperty(entry.Key.ToString()).SetValue(ObjEntity, Double.Parse(entry.Value.ToString()), null); 
                                        break; 
                                        case "System.Int16": 
                                                ObjValue.GetProperty(entry.Key.ToString()).SetValue(ObjEntity, Int16.Parse(entry.Value.ToString()), null); 
                                        break; 
                                        case "System.Int32": 
                                                ObjValue.GetProperty(entry.Key.ToString()).SetValue(ObjEntity, Int32.Parse(entry.Value.ToString()), null); 
                                        break; 
                                        case "System.Int64": 
                                                ObjValue.GetProperty(entry.Key.ToString()).SetValue(ObjEntity, Int64.Parse(entry.Value.ToString()), null); 
                                        break; 
                                        default: 
                                                ObjValue.GetProperty(entry.Key.ToString()).SetValue(ObjEntity, entry.Value.ToString(), null); 
                                        break; 
                                } 
                        } 
                        catch 
                        { 
                                if (bNullable == true) 
                                { 
                                        ObjValue.GetProperty(entry.Key.ToString()).SetValue(ObjEntity, null, null); 
                                } 
                        } 
                } 
        }
}

static public void ParseFormViewInsertValue(object ObjEntity, FormViewInsertEventArgs e)

        ParseFormViewValue(ObjEntity, e.Values);
}

static public void ParseFormViewDeleteValue(object ObjEntity, FormViewDeleteEventArgs e)

        ParseFormViewValue(ObjEntity, e.Values);
}

static public void ParseFormViewUpdateOldValue(object ObjEntity, FormViewUpdateEventArgs e)

        ParseFormViewValue(ObjEntity, e.OldValues);
}

static public void ParseFormViewUpdateNewValue(object ObjEntity, FormViewUpdateEventArgs e)

        ParseFormViewValue(ObjEntity, e.NewValues);
}

#region parse control value and set value to object;set object value to control
        static public void WriteObjectDataToControl(object ObjEntity, Control ContainerView)
        {
            SetObjectToControls(ObjEntity, ContainerView);
        }
        static private void SetObjectToControls(object ObjEntity, Control InControls)
        {
            foreach (Control ctl in InControls.Controls)
            {
                if (ctl.Controls.Count > 1)
                {
                    SetObjectToControls(ObjEntity, ctl);
                }
                PropertyInfo[] ObjFields = ObjEntity.GetType().GetProperties();
                foreach (PropertyInfo ObjFeildsProperty in ObjFields)
                {
                    if ((ctl.ID != null) && (ctl.ID.Contains(ObjFeildsProperty.Name)))
                    {
                        string StrDateType = ObjFeildsProperty.PropertyType.ToString();
                        Boolean bNullable = StrDateType.Contains("System.Nullable`1");
                        if (bNullable == true)
                            StrDateType = StrDateType.Replace("System.Nullable`1", "").Replace("[", "").Replace("]", "");
                        switch (ctl.GetType().Name)
                        {
                            case "TextBox":
                                (ctl as TextBox).Text = GetControlValue(ObjFeildsProperty.GetValue(ObjEntity, null), StrDateType, bNullable).ToString();
                                break;
                            case "CheckBox":
                                (ctl as CheckBox).Checked = Boolean.Parse(GetControlValue(ObjFeildsProperty.GetValue(ObjEntity, null), StrDateType, bNullable).ToString());
                                break;
                            case "CheckBoxList":
                                (ctl as CheckBoxList).SelectedIndex = int.Parse(GetControlValue(ObjFeildsProperty.GetValue(ObjEntity, null), StrDateType, bNullable).ToString());
                                break;
                            case "DropDownList":
                                (ctl as DropDownList).SelectedValue = GetControlValue(ObjFeildsProperty.GetValue(ObjEntity, null), StrDateType, bNullable).ToString();
                                break;
                            case "RadioButton":
                                (ctl as RadioButton).Checked = Boolean.Parse(GetControlValue(ObjFeildsProperty.GetValue(ObjEntity, null), StrDateType, bNullable).ToString());
                                break;
                            case "RadioButtonList":
                                (ctl as RadioButtonList).SelectedIndex = int.Parse(GetControlValue(ObjFeildsProperty.GetValue(ObjEntity, null), StrDateType, bNullable).ToString());
                                break;
                            case "Label":
                                (ctl as Label).Text = GetControlValue(ObjFeildsProperty.GetValue(ObjEntity, null), StrDateType, bNullable).ToString();
                                break;
                        }
                    }
                }
            }
        }
        static private object GetControlValue(object ObjValue, string StrDataType, Boolean bNullable)
        {
            object objResult = null;
            try
            {
                switch (StrDataType)
                {
                    case "System.DateTime":
                        if (ObjValue == null)
                            objResult = "";
                        else
                            objResult = DateTime.Parse(ObjValue.ToString());
                        break;
                    case "System.Boolean":
                        if (ObjValue == null)
                            objResult = false;
                        else
                            objResult = Boolean.Parse(ObjValue.ToString());
                        break;
                    case "System.Decimal":
                        if (ObjValue == null)
                            objResult = 0;
                        else
                            objResult = Decimal.Parse(ObjValue.ToString());
                        break;
                    case "System.Double":
                        if (ObjValue == null)
                            objResult = 0;
                        else
                            objResult = Double.Parse(ObjValue.ToString());
                        break;
                    case "System.Int16":
                        if (ObjValue == null)
                            objResult = 0;
                        else
                            objResult = Int16.Parse(ObjValue.ToString());
                        break;
                    case "System.Int32":
                        if (ObjValue == null)
                            objResult = 0;
                        else
                            objResult = Int32.Parse(ObjValue.ToString());
                        break;
                    case "System.Int64":
                        if (ObjValue == null)
                            objResult = 0;
                        else
                            objResult = Int64.Parse(ObjValue.ToString());
                        break;
                    default:
                        if (ObjValue == null)
                            objResult = "";
                        else
                            objResult = ObjValue.ToString();
                        break;
                }
            }
            catch
            {
            }
            return objResult;
        }
        static public void ParseControlDataToObject(Control ContainerView, object ObjEntity)
        {
            EnumContaionerControls(ContainerView, ObjEntity);
        }
        static private void EnumContaionerControls(Control InControls, object ObjEntity)
        {
            foreach (Control ctl in InControls.Controls)
            {
                if (ctl.Controls.Count > 1)
                {
                    EnumContaionerControls(ctl, ObjEntity);
                }
                PropertyInfo[] ObjFields = ObjEntity.GetType().GetProperties();
                foreach (PropertyInfo ObjFeildsProperty in ObjFields)
                {
                    if ((ctl.ID != null) && (ctl.ID.Contains(ObjFeildsProperty.Name)))
                    {
                        string StrDateType = ObjFeildsProperty.PropertyType.ToString();
                        Boolean bNullable = StrDateType.Contains("System.Nullable`1");
                        if (bNullable == true)
                            StrDateType = StrDateType.Replace("System.Nullable`1", "").Replace("[", "").Replace("]", "");
                        switch (ctl.GetType().Name)
                        {
                            case "TextBox":
                                SetControlValueToObject(ObjEntity, ObjFeildsProperty, (ctl as TextBox).Text.ToString(), StrDateType, bNullable);
                                break;
                            case "CheckBox":
                                SetControlValueToObject(ObjEntity, ObjFeildsProperty, (ctl as CheckBox).Checked.ToString(), StrDateType, bNullable);
                                break;
                            case "CheckBoxList":
                                SetControlValueToObject(ObjEntity, ObjFeildsProperty, (ctl as CheckBoxList).SelectedIndex.ToString(), StrDateType, bNullable);
                                break;
                            case "DropDownList":
                                SetControlValueToObject(ObjEntity, ObjFeildsProperty, (ctl as DropDownList).SelectedValue.ToString(), StrDateType, bNullable);
                                break;
                            case "RadioButton":
                                SetControlValueToObject(ObjEntity, ObjFeildsProperty, (ctl as RadioButton).Checked.ToString(), StrDateType, bNullable);
                                break;
                            case "RadioButtonList":
                                SetControlValueToObject(ObjEntity, ObjFeildsProperty, (ctl as RadioButtonList).SelectedIndex.ToString(), StrDateType, bNullable);
                                break;
                            case "Label":
                                SetControlValueToObject(ObjEntity, ObjFeildsProperty, (ctl as Label).Text.ToString(), StrDateType, bNullable);
                                break;
                        }
                    }
                }
            }
        }
        static private void SetControlValueToObject(object ObjEntity, PropertyInfo ObjFeildsProperty,
            string StrValue, string StrDataType, Boolean bNullable)
        {
            try
            {
                switch (StrDataType)
                {
                    case "System.DateTime":
                        ObjFeildsProperty.SetValue(ObjEntity, DateTime.Parse(StrValue), null);
                        break;
                    case "System.Boolean":
                        ObjFeildsProperty.SetValue(ObjEntity, Boolean.Parse(StrValue), null);
                        break;
                    case "System.Decimal":
                        ObjFeildsProperty.SetValue(ObjEntity, Decimal.Parse(StrValue), null);
                        break;
                    case "System.Double":
                        ObjFeildsProperty.SetValue(ObjEntity, Double.Parse(StrValue), null);
                        break;
                    case "System.Int16":
                        ObjFeildsProperty.SetValue(ObjEntity, Int16.Parse(StrValue), null);
                        break;
                    case "System.Int32":
                        ObjFeildsProperty.SetValue(ObjEntity, Int32.Parse(StrValue), null);
                        break;
                    case "System.Int64":
                        ObjFeildsProperty.SetValue(ObjEntity, Int64.Parse(StrValue), null);
                        break;
                    default:
                        ObjFeildsProperty.SetValue(ObjEntity, StrValue, null);
                        break;
                }
            }
            catch
            {
                if (bNullable == true)
                {
                    ObjFeildsProperty.SetValue(ObjEntity, null, null);
                }
            }
        }
        #endregion parse control value and set value to object;set object value to control

Sample:
protected void fvFormView_ItemInserting(object sender, FormViewInsertEventArgs e)

        //UserInfo usr = new UserInfo(); 
        //ParseFormViewInsertValue(usr, e); 
        //usr.Remark = "hello world"; 
        ////Place some rule here. 
        //WriteFormViewInsertValue(usr, e);
}
只有注册用户登录后才能发表评论。