MActionUI.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Text;
  3. using System.Web.UI.WebControls;
  4. using System.Web.UI;
  5. using Win = System.Windows.Forms;
  6. using Ant.ORM.Table;
  7. using System.Collections.Generic;
  8. using Ant.ORM.SQL;
  9. using System.Data;
  10. using System.ComponentModel;
  11. namespace Ant.ORM
  12. {
  13. internal class MActionUI:IDisposable
  14. {
  15. private List<string> autoPrefixList;//调用插入和更新,自动获取控件名的前缀
  16. public MDataRow _Row;
  17. public MActionUI(ref MDataRow row)
  18. {
  19. _Row = row;
  20. }
  21. #region UI操作分路
  22. public void Set(object ct, object value, bool isControlEnabled)
  23. {
  24. if (ct is Control)
  25. {
  26. SetTo(ct as Control, value, isControlEnabled);
  27. }
  28. else
  29. {
  30. SetTo(ct as Win.Control, value, isControlEnabled);
  31. }
  32. }
  33. public void Get(object ct, object value)
  34. {
  35. if (ct is Control)
  36. {
  37. GetFrom(ct as Control, value);
  38. }
  39. else
  40. {
  41. GetFrom(ct as Win.Control, value);
  42. }
  43. }
  44. #endregion
  45. #region WebUI操作
  46. public void SetTo(Control ct, object value, bool isControlEnabled)
  47. {
  48. string propName = ct.ID.Substring(3);
  49. if (value == null)
  50. {
  51. value = _Row[propName].Value;
  52. }
  53. switch (ct.GetType().Name)
  54. {
  55. case "TextBox":
  56. ((TextBox)ct).Text = Convert.ToString(value);
  57. ((TextBox)ct).Enabled = isControlEnabled;
  58. break;
  59. case "Literal":
  60. ((Literal)ct).Text = Convert.ToString(value);
  61. break;
  62. case "Label":
  63. ((Label)ct).Text = Convert.ToString(value);
  64. break;
  65. case "HiddenField":
  66. ((HiddenField)ct).Value = Convert.ToString(value);
  67. break;
  68. case "DropDownList":
  69. ((DropDownList)ct).SelectedValue = Convert.ToString(value);
  70. ((DropDownList)ct).Enabled = isControlEnabled;
  71. break;
  72. case "CheckBox":
  73. bool tempValue;
  74. if (Convert.ToString(value) == "1")
  75. {
  76. tempValue = true;
  77. }
  78. else
  79. {
  80. bool.TryParse(Convert.ToString(value), out tempValue);
  81. }
  82. ((CheckBox)ct).Checked = tempValue;
  83. ((CheckBox)ct).Enabled = isControlEnabled;
  84. break;
  85. }
  86. }
  87. public void GetFrom(Control ct, object value)
  88. {
  89. string propName = ct.ID.Substring(3);
  90. if (value == null)
  91. {
  92. switch (ct.GetType().Name)
  93. {
  94. case "TextBox":
  95. value = ((TextBox)ct).Text.Trim();
  96. break;
  97. case "Literal":
  98. value = ((Literal)ct).Text;
  99. break;
  100. case "Label":
  101. value = ((Label)ct).Text;
  102. break;
  103. case "HiddenField":
  104. value = ((HiddenField)ct).Value;
  105. break;
  106. case "DropDownList":
  107. value = ((DropDownList)ct).SelectedValue;
  108. break;
  109. case "CheckBox":
  110. value = ((CheckBox)ct).Checked;
  111. break;
  112. }
  113. }
  114. _Row[propName].Value = value;
  115. }
  116. #endregion
  117. #region WinUI操作
  118. public void SetTo(Win.Control ct, object value, bool isControlEnabled)
  119. {
  120. string propName = ct.Name.Substring(3);
  121. if (value == null)
  122. {
  123. value = _Row[propName].Value;
  124. }
  125. switch (ct.GetType().Name)
  126. {
  127. case "TextBox":
  128. ((Win.TextBox)ct).Text = Convert.ToString(value);
  129. ((Win.TextBox)ct).Enabled = isControlEnabled;
  130. break;
  131. case "ComboBox":
  132. ((Win.ComboBox)ct).Items.Add(value);
  133. break;
  134. case "Label":
  135. ((Win.Label)ct).Text = Convert.ToString(value);
  136. break;
  137. case "DateTimePicker":
  138. DateTime dt;
  139. if (DateTime.TryParse(Convert.ToString(value), out dt))
  140. {
  141. ((Win.DateTimePicker)ct).Value = dt;
  142. }
  143. break;
  144. case "ListBox":
  145. ((Win.ListBox)ct).Items.Add(value);
  146. break;
  147. case "CheckBox":
  148. bool tempValue;
  149. if (Convert.ToString(value) == "1")
  150. {
  151. tempValue = true;
  152. }
  153. else
  154. {
  155. bool.TryParse(Convert.ToString(value), out tempValue);
  156. }
  157. ((Win.CheckBox)ct).Checked = tempValue;
  158. ((Win.CheckBox)ct).Enabled = isControlEnabled;
  159. break;
  160. case "NumericUpDown":
  161. decimal result = 0;
  162. if (decimal.TryParse(Convert.ToString(value), out result))
  163. {
  164. ((Win.NumericUpDown)ct).Value = result;
  165. }
  166. break;
  167. case "RichTextBox":
  168. ((Win.ListBox)ct).Text = Convert.ToString(value);
  169. break;
  170. }
  171. }
  172. public void GetFrom(Win.Control ct, object value)
  173. {
  174. string propName = ct.Name.Substring(3);
  175. if (value == null)
  176. {
  177. switch (ct.GetType().Name)
  178. {
  179. case "TextBox":
  180. value = ((Win.TextBox)ct).Text.Trim();
  181. break;
  182. case "ComboBox":
  183. value = ((Win.ComboBox)ct).Text;
  184. break;
  185. case "Label":
  186. value = ((Win.Label)ct).Text;
  187. break;
  188. case "DateTimePicker":
  189. value = ((Win.DateTimePicker)ct).Value;
  190. break;
  191. case "ListBox":
  192. value = ((Win.ListBox)ct).Text;
  193. break;
  194. case "CheckBox":
  195. value = ((Win.CheckBox)ct).Checked;
  196. break;
  197. case "NumericUpDown":
  198. value = ((Win.NumericUpDown)ct).Value;
  199. break;
  200. case "RichTextBox":
  201. value = ((Win.RichTextBox)ct).Text;
  202. break;
  203. }
  204. }
  205. _Row[propName].Value = value;
  206. }
  207. #endregion
  208. #region Web自动取值
  209. /// <summary>
  210. /// 自动设置列的值(true为插入,false为更新)
  211. /// </summary>
  212. public void AutoSetColumnValue(bool containsID)
  213. {
  214. // Type type = null;
  215. int i = 0;
  216. if (containsID || !_Row[0]._CellValue.IsNull)
  217. {
  218. i = 1;
  219. }
  220. for (; i < _Row.Count; i++)
  221. {
  222. if (!_Row[i]._CellValue.IsChange)
  223. {
  224. try
  225. {
  226. foreach (string autoPrefix in autoPrefixList)
  227. {
  228. string RequestValue = System.Web.HttpContext.Current.Request[autoPrefix + _Row[i]._CellStruct.ColumnName];
  229. if (RequestValue != null)
  230. {
  231. if (RequestValue == "on")
  232. {
  233. if (_Row[i]._CellStruct.SqlType == SqlDbType.Bit)
  234. {
  235. _Row[i].Value = true;
  236. }
  237. else
  238. {
  239. _Row[i].Value = 1;
  240. }
  241. break;
  242. }
  243. if (RequestValue.Length == 0 && DataType.GetGroupID(_Row[i]._CellStruct.SqlType) == 1)
  244. {
  245. _Row[i].Value = 0;
  246. break;
  247. }
  248. else if (_Row[i]._CellStruct.SqlType == SqlDbType.Bit && RequestValue.Length==1)
  249. {
  250. _Row[i].Value = RequestValue == "1";
  251. }
  252. _Row[i].Value = TypeDescriptor.GetConverter(_Row[i]._CellStruct.ValueType).ConvertFrom(RequestValue.Trim());
  253. break;
  254. }
  255. }
  256. }
  257. catch
  258. {
  259. }
  260. }
  261. }
  262. }
  263. #endregion
  264. #region 其它方法
  265. public void SetAutoPrefix(string autoPrefix, params string[] otherPrefix)
  266. {
  267. autoPrefixList = new List<string>();
  268. autoPrefixList.Add(autoPrefix);
  269. foreach (string item in otherPrefix)
  270. {
  271. autoPrefixList.Add(item);
  272. }
  273. }
  274. #endregion
  275. #region IDisposable 成员
  276. public void Dispose()
  277. {
  278. if (autoPrefixList != null)
  279. {
  280. autoPrefixList.Clear();
  281. autoPrefixList = null;
  282. }
  283. }
  284. #endregion
  285. }
  286. }