ExtControl.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using ETDExtUI;
  7. namespace ETD.Frame
  8. {
  9. public class ExtControl : BaseControl
  10. {
  11. System.Web.UI.Control control = new System.Web.UI.Control();
  12. public ExtControl(System.Web.UI.Control con)
  13. {
  14. control = con;
  15. }
  16. public override int SetterControl(List<object> entity, string lbl)
  17. {
  18. //Type ControlType = control.GetType();
  19. //switch (ControlType.Name)
  20. //{
  21. // case "TextBox":
  22. // if (!string.IsNullOrEmpty((control as TextBox).ID))
  23. // {
  24. // foreach (object objj in entity)
  25. // {
  26. // //Type t = Type.GetType(objj, true);
  27. // System.Reflection.PropertyInfo propertyInfo = t.GetProperty((control as TextBox).ID);
  28. // if (propertyInfo != null)
  29. // {
  30. // string tempText = (control as TextBox).Text;
  31. // if (!string.IsNullOrEmpty(tempText))
  32. // propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
  33. // }
  34. // }
  35. // }
  36. // break;
  37. //}
  38. #region TextBox
  39. TextBox txtcon = new TextBox();
  40. if (string.IsNullOrEmpty(lbl))
  41. lbl = "_";
  42. if (control is TextBox && control.ClientID.Contains(lbl))
  43. {
  44. txtcon = (TextBox)control;
  45. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  46. string str = "";
  47. foreach (object obj in entity)
  48. {
  49. if (string.IsNullOrEmpty(str))
  50. {
  51. try
  52. {
  53. str = obj.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, obj, null).ToString();
  54. //str = obj.GetType().InvokeMember(fieldName, BindingFlags.IgnoreCase | //忽略大小写 指定当绑定时不应考虑成员名的大小写
  55. //BindingFlags.GetProperty, null, obj, null).ToString();
  56. }
  57. catch { str = ""; }
  58. }
  59. else
  60. break;
  61. }
  62. if (!string.IsNullOrEmpty(str))
  63. {
  64. txtcon.Text = str;
  65. return 0;
  66. }
  67. }
  68. #endregion
  69. #region TextArea
  70. TextArea txt = new TextArea();
  71. if (control is TextArea && control.ClientID.Contains(lbl))
  72. {
  73. txt = (TextArea)control;
  74. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  75. string str = "";
  76. foreach (object obj in entity)
  77. {
  78. if (string.IsNullOrEmpty(str))
  79. {
  80. try
  81. {
  82. str = obj.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, obj, null).ToString();
  83. }
  84. catch { str = ""; }
  85. }
  86. else
  87. break;
  88. }
  89. if (!string.IsNullOrEmpty(str))
  90. {
  91. txt.Text = str;
  92. return 0;
  93. }
  94. }
  95. #endregion
  96. #region DropDownList
  97. DropDownList ddl = new DropDownList();
  98. if (control is DropDownList && control.ClientID.Contains(lbl))
  99. {
  100. ddl = (DropDownList)control;
  101. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  102. string str = "";
  103. try
  104. {
  105. str = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
  106. }
  107. catch { }
  108. if (!string.IsNullOrEmpty(str))
  109. {
  110. ddl.SelectedValue = str;
  111. return 0;
  112. }
  113. }
  114. #endregion
  115. #region CheckBox
  116. CheckBox chk = new CheckBox();
  117. if (control is CheckBox && control.ClientID.Contains(lbl))
  118. {
  119. chk = (CheckBox)control;
  120. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  121. string str = "";
  122. try
  123. {
  124. str = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
  125. }
  126. catch { }
  127. if (!string.IsNullOrEmpty(str))
  128. {
  129. if (str == "1")
  130. chk.Checked = true;
  131. if (str == "0")
  132. chk.Checked = false;
  133. if (str.ToLower() == "true")
  134. chk.Checked = true;
  135. if (str.ToLower() == "false")
  136. chk.Checked = false;
  137. return 0;
  138. }
  139. }
  140. #endregion
  141. return 1;
  142. }
  143. public override List<object> GetterControl(List<object> entity, string lbl)
  144. {
  145. #region TextBox
  146. TextBox txtcon = new TextBox(); List<object> objlist = new List<object>();
  147. // object objj = Activator.CreateInstance(entity.GetType());
  148. if (string.IsNullOrEmpty(lbl))
  149. lbl = "_";
  150. if (control is TextBox && control.ClientID.Contains(lbl))
  151. {
  152. txtcon = (TextBox)control;
  153. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  154. // PropertyInfo[] propertys = objj.GetType().GetProperties();
  155. foreach (object obj in entity)
  156. {
  157. //object objj = Activator.CreateInstance(obj.GetType());
  158. PropertyInfo pi = obj.GetType().GetProperty(fieldName);
  159. if (!object.Equals(pi, null))
  160. {
  161. try
  162. {
  163. if ((Object.Equals(txtcon.Text, null)) || (Object.Equals(txtcon.Text, DBNull.Value)))
  164. pi.SetValue(obj, null, null);//为空,但不为Null
  165. else
  166. pi.SetValue(obj, txtcon.Text, null);
  167. }
  168. catch
  169. {
  170. pi.SetValue(obj, null, null);//为空,但不为Null
  171. }
  172. objlist.Add(obj);
  173. }
  174. }
  175. }
  176. #endregion
  177. return objlist;
  178. }
  179. public override T GetterControl<T>(T entity, string lbl)
  180. {
  181. #region TextBox
  182. TextBox txtcon = new TextBox(); List<T> objlist = new List<T>();
  183. T objj = Activator.CreateInstance<T>(); objj = entity;
  184. // object objj = Activator.CreateInstance(entity.GetType());
  185. if (string.IsNullOrEmpty(lbl))
  186. lbl = "_";
  187. if (control is TextBox && control.ClientID.Contains(lbl))
  188. {
  189. txtcon = (TextBox)control;
  190. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  191. // PropertyInfo[] propertys = objj.GetType().GetProperties();
  192. PropertyInfo pi = entity.GetType().GetProperty(fieldName, BindingFlags.Public | BindingFlags.NonPublic
  193. | BindingFlags.Instance | BindingFlags.IgnoreCase);
  194. if (!object.Equals(pi, null))
  195. {
  196. try
  197. {
  198. if ((Object.Equals(txtcon.Text, null)) || (Object.Equals(txtcon.Text, DBNull.Value)))
  199. pi.SetValue(objj, null, null);//为空,但不为Null
  200. else
  201. pi.SetValue(objj, txtcon.Text, null);
  202. }
  203. catch
  204. {
  205. pi.SetValue(objj, null, null);//为空,但不为Null
  206. }
  207. }
  208. }
  209. #endregion
  210. #region TextArea
  211. TextArea txt = new TextArea();
  212. if (control is TextArea && control.ClientID.Contains(lbl))
  213. {
  214. txt = (TextArea)control;
  215. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  216. // PropertyInfo[] propertys = objj.GetType().GetProperties();
  217. PropertyInfo pi = entity.GetType().GetProperty(fieldName);
  218. if (!object.Equals(pi, null))
  219. {
  220. try
  221. {
  222. if ((Object.Equals(txt.Text, null)) || (Object.Equals(txt.Text, DBNull.Value)))
  223. pi.SetValue(objj, null, null);//为空,但不为Null
  224. else
  225. pi.SetValue(objj, txt.Text, null);
  226. }
  227. catch
  228. {
  229. pi.SetValue(objj, null, null);//为空,但不为Null
  230. }
  231. }
  232. }
  233. #endregion
  234. return objj;
  235. }
  236. }
  237. }