123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Reflection;
- using ETDExtUI;
- namespace ETD.Frame
- {
- public class ExtControl : BaseControl
- {
- System.Web.UI.Control control = new System.Web.UI.Control();
- public ExtControl(System.Web.UI.Control con)
- {
- control = con;
- }
- public override int SetterControl(List<object> entity, string lbl)
- {
- //Type ControlType = control.GetType();
- //switch (ControlType.Name)
- //{
- // case "TextBox":
- // if (!string.IsNullOrEmpty((control as TextBox).ID))
- // {
- // foreach (object objj in entity)
- // {
- // //Type t = Type.GetType(objj, true);
- // System.Reflection.PropertyInfo propertyInfo = t.GetProperty((control as TextBox).ID);
- // if (propertyInfo != null)
- // {
- // string tempText = (control as TextBox).Text;
- // if (!string.IsNullOrEmpty(tempText))
- // propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
- // }
- // }
- // }
- // break;
- //}
- #region TextBox
- TextBox txtcon = new TextBox();
- if (string.IsNullOrEmpty(lbl))
- lbl = "_";
- if (control is TextBox && control.ClientID.Contains(lbl))
- {
- txtcon = (TextBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- string str = "";
- foreach (object obj in entity)
- {
- if (string.IsNullOrEmpty(str))
- {
- try
- {
- str = obj.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, obj, null).ToString();
- //str = obj.GetType().InvokeMember(fieldName, BindingFlags.IgnoreCase | //忽略大小写 指定当绑定时不应考虑成员名的大小写
- //BindingFlags.GetProperty, null, obj, null).ToString();
- }
- catch { str = ""; }
- }
- else
- break;
- }
- if (!string.IsNullOrEmpty(str))
- {
- txtcon.Text = str;
- return 0;
- }
- }
- #endregion
- #region TextArea
- TextArea txt = new TextArea();
- if (control is TextArea && control.ClientID.Contains(lbl))
- {
- txt = (TextArea)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- string str = "";
- foreach (object obj in entity)
- {
- if (string.IsNullOrEmpty(str))
- {
- try
- {
- str = obj.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, obj, null).ToString();
- }
- catch { str = ""; }
- }
- else
- break;
- }
- if (!string.IsNullOrEmpty(str))
- {
- txt.Text = str;
- return 0;
- }
- }
- #endregion
- #region DropDownList
- DropDownList ddl = new DropDownList();
- if (control is DropDownList && control.ClientID.Contains(lbl))
- {
- ddl = (DropDownList)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- string str = "";
- try
- {
- str = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
- }
- catch { }
- if (!string.IsNullOrEmpty(str))
- {
- ddl.SelectedValue = str;
- return 0;
- }
- }
- #endregion
- #region CheckBox
- CheckBox chk = new CheckBox();
- if (control is CheckBox && control.ClientID.Contains(lbl))
- {
- chk = (CheckBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- string str = "";
- try
- {
- str = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
- }
- catch { }
- if (!string.IsNullOrEmpty(str))
- {
- if (str == "1")
- chk.Checked = true;
- if (str == "0")
- chk.Checked = false;
- if (str.ToLower() == "true")
- chk.Checked = true;
- if (str.ToLower() == "false")
- chk.Checked = false;
- return 0;
- }
- }
- #endregion
- return 1;
- }
- public override List<object> GetterControl(List<object> entity, string lbl)
- {
- #region TextBox
- TextBox txtcon = new TextBox(); List<object> objlist = new List<object>();
- // object objj = Activator.CreateInstance(entity.GetType());
- if (string.IsNullOrEmpty(lbl))
- lbl = "_";
- if (control is TextBox && control.ClientID.Contains(lbl))
- {
- txtcon = (TextBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- // PropertyInfo[] propertys = objj.GetType().GetProperties();
- foreach (object obj in entity)
- {
- //object objj = Activator.CreateInstance(obj.GetType());
- PropertyInfo pi = obj.GetType().GetProperty(fieldName);
- if (!object.Equals(pi, null))
- {
- try
- {
- if ((Object.Equals(txtcon.Text, null)) || (Object.Equals(txtcon.Text, DBNull.Value)))
- pi.SetValue(obj, null, null);//为空,但不为Null
- else
- pi.SetValue(obj, txtcon.Text, null);
- }
- catch
- {
- pi.SetValue(obj, null, null);//为空,但不为Null
- }
- objlist.Add(obj);
- }
- }
- }
- #endregion
- return objlist;
- }
- public override T GetterControl<T>(T entity, string lbl)
- {
- #region TextBox
- TextBox txtcon = new TextBox(); List<T> objlist = new List<T>();
- T objj = Activator.CreateInstance<T>(); objj = entity;
- // object objj = Activator.CreateInstance(entity.GetType());
- if (string.IsNullOrEmpty(lbl))
- lbl = "_";
- if (control is TextBox && control.ClientID.Contains(lbl))
- {
- txtcon = (TextBox)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- // PropertyInfo[] propertys = objj.GetType().GetProperties();
- PropertyInfo pi = entity.GetType().GetProperty(fieldName, BindingFlags.Public | BindingFlags.NonPublic
- | BindingFlags.Instance | BindingFlags.IgnoreCase);
- if (!object.Equals(pi, null))
- {
- try
- {
- if ((Object.Equals(txtcon.Text, null)) || (Object.Equals(txtcon.Text, DBNull.Value)))
- pi.SetValue(objj, null, null);//为空,但不为Null
- else
- pi.SetValue(objj, txtcon.Text, null);
- }
- catch
- {
- pi.SetValue(objj, null, null);//为空,但不为Null
- }
- }
- }
- #endregion
- #region TextArea
- TextArea txt = new TextArea();
- if (control is TextArea && control.ClientID.Contains(lbl))
- {
- txt = (TextArea)control;
- string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
- // PropertyInfo[] propertys = objj.GetType().GetProperties();
- PropertyInfo pi = entity.GetType().GetProperty(fieldName);
- if (!object.Equals(pi, null))
- {
- try
- {
- if ((Object.Equals(txt.Text, null)) || (Object.Equals(txt.Text, DBNull.Value)))
- pi.SetValue(objj, null, null);//为空,但不为Null
- else
- pi.SetValue(objj, txt.Text, null);
- }
- catch
- {
- pi.SetValue(objj, null, null);//为空,但不为Null
- }
- }
- }
- #endregion
- return objj;
- }
- }
- }
|