NetControl.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. namespace Ant.Frame
  9. {
  10. public class NetControl : BaseControl
  11. {
  12. Control control = new Control();
  13. public NetControl(Control con)
  14. {
  15. control = con;
  16. }
  17. /// <summary>
  18. /// Lable控件
  19. /// </summary>
  20. /// <param name="entity"></param>
  21. /// <param name="lbl"></param>
  22. /// <returns></returns>
  23. public override int SetterControl(List<object> entity, string lbl)
  24. {
  25. if (string.IsNullOrEmpty(lbl))
  26. lbl = "_";
  27. Label lblcon = new Label();
  28. if (control is Label)
  29. {
  30. lblcon = (Label)control;
  31. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  32. string str = "";
  33. foreach (object obj in entity)
  34. {
  35. if (string.IsNullOrEmpty(str))
  36. {
  37. try
  38. {
  39. str = obj.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, obj, null).ToString();
  40. }
  41. catch { str = ""; }
  42. }
  43. else
  44. break;
  45. }
  46. if (!string.IsNullOrEmpty(str))
  47. {
  48. lblcon.Text = str;
  49. return 0;
  50. }
  51. }
  52. #region TextBox
  53. TextBox txtcon = new TextBox();
  54. if (control is TextBox)
  55. {
  56. txtcon = (TextBox)control;
  57. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  58. string str = "";
  59. foreach (object obj in entity)
  60. {
  61. if (string.IsNullOrEmpty(str))
  62. {
  63. try
  64. {
  65. str = obj.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, obj, null).ToString();
  66. }
  67. catch { str = ""; }
  68. }
  69. else
  70. break;
  71. }
  72. if (!string.IsNullOrEmpty(str))
  73. {
  74. txtcon.Text = str;
  75. return 0;
  76. }
  77. }
  78. #endregion
  79. #region DropDownList
  80. DropDownList ddl = new DropDownList();
  81. if (control is DropDownList && control.ClientID.Contains(lbl))
  82. {
  83. ddl = (DropDownList)control;
  84. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  85. string str = "";
  86. try
  87. {
  88. str = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
  89. }
  90. catch { }
  91. if (!string.IsNullOrEmpty(str))
  92. {
  93. ddl.SelectedValue = str;
  94. return 0;
  95. }
  96. }
  97. #endregion
  98. #region CheckBox
  99. CheckBox chk = new CheckBox();
  100. if (control is CheckBox && control.ClientID.Contains(lbl))
  101. {
  102. chk = (CheckBox)control;
  103. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  104. string str = "";
  105. try
  106. {
  107. str = entity.GetType().InvokeMember(fieldName, BindingFlags.GetProperty, null, entity, null).ToString();
  108. }
  109. catch { }
  110. if (!string.IsNullOrEmpty(str))
  111. {
  112. if (str == "1")
  113. chk.Checked = true;
  114. if (str == "0")
  115. chk.Checked = false;
  116. if (str.ToLower() == "true")
  117. chk.Checked = true;
  118. if (str.ToLower() == "false")
  119. chk.Checked = false;
  120. return 0;
  121. }
  122. }
  123. #endregion
  124. return 0;
  125. }
  126. public override T GetterControl<T>(T entity, string lbl)
  127. {
  128. if (string.IsNullOrEmpty(lbl))
  129. lbl = "_";
  130. #region TextBox
  131. TextBox txtcon = new TextBox(); List<T> objlist = new List<T>();
  132. T objj = Activator.CreateInstance<T>(); objj = entity;
  133. // object objj = Activator.CreateInstance(entity.GetType());
  134. if (control is TextBox && control.ClientID.Contains(lbl))
  135. {
  136. txtcon = (TextBox)control;
  137. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  138. // PropertyInfo[] propertys = objj.GetType().GetProperties();
  139. PropertyInfo pi = entity.GetType().GetProperty(fieldName, BindingFlags.Public | BindingFlags.NonPublic
  140. | BindingFlags.Instance | BindingFlags.IgnoreCase);
  141. if (!object.Equals(pi, null))
  142. {
  143. try
  144. {
  145. if ((Object.Equals(txtcon.Text, null)) || (Object.Equals(txtcon.Text, DBNull.Value)))
  146. pi.SetValue(objj, null, null);//为空,但不为Null
  147. else
  148. pi.SetValue(objj, txtcon.Text, null);
  149. }
  150. catch
  151. {
  152. pi.SetValue(objj, null, null);//为空,但不为Null
  153. }
  154. }
  155. }
  156. #endregion
  157. return objj;
  158. }
  159. public override List<object> GetterControl(List<object> entity, string lbl)
  160. {
  161. if (string.IsNullOrEmpty(lbl))
  162. lbl = "_";
  163. #region TextBox
  164. TextBox txtcon = new TextBox(); List<object> objlist = new List<object>();
  165. // object objj = Activator.CreateInstance(entity.GetType());
  166. if (control is TextBox && control.ClientID.Contains(lbl))
  167. {
  168. txtcon = (TextBox)control;
  169. string fieldName = control.ClientID.Split('_')[(control.ClientID.Split('_').Length) - 1];
  170. // PropertyInfo[] propertys = objj.GetType().GetProperties();
  171. foreach (object obj in entity)
  172. {
  173. //object objj = Activator.CreateInstance(obj.GetType());
  174. PropertyInfo pi = obj.GetType().GetProperty(fieldName);
  175. if (!object.Equals(pi, null))
  176. {
  177. try
  178. {
  179. if ((Object.Equals(txtcon.Text, null)) || (Object.Equals(txtcon.Text, DBNull.Value)))
  180. pi.SetValue(obj, null, null);//为空,但不为Null
  181. else
  182. pi.SetValue(obj, txtcon.Text, null);
  183. }
  184. catch
  185. {
  186. pi.SetValue(obj, null, null);//为空,但不为Null
  187. }
  188. objlist.Add(obj);
  189. }
  190. }
  191. }
  192. #endregion
  193. return objlist;
  194. }
  195. }
  196. }