DymWebFormControlMappnig.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. namespace DymWebForm.Aspx
  7. {
  8. public class DymWebFormControlMappnig
  9. {
  10. private readonly Type _typControl;
  11. private readonly Action<object> _delInitControl;
  12. public Action<object, object> ControlSetValue { get; set; }
  13. public Func<object, object> ControlGetValue { get; private set; }
  14. public DymWebFormValidation Validation { get; set; }
  15. //
  16. internal WebControl CreateControl()
  17. {
  18. var c = (WebControl)Activator.CreateInstance(_typControl);
  19. if (_delInitControl != null)
  20. _delInitControl(c);
  21. return c;
  22. }
  23. //
  24. private DymWebFormControlMappnig(Type controlType, Action<object> controlInit, Action<object, object> controlSetValue, Func<object, object> controlGetValue, DymWebFormValidation validation)
  25. {
  26. this._typControl = controlType;
  27. this._delInitControl = controlInit;
  28. this.ControlSetValue = controlSetValue;
  29. this.ControlGetValue = controlGetValue;
  30. this.Validation = validation;
  31. }
  32. //
  33. private static Dictionary<Type, Dictionary<Type, DymWebFormControlMappnig>> ms_dicMapping;
  34. public static void SetControlMapping<TDataType, TControl, TValidation>(
  35. Action<TControl, TValidation> init
  36. , Action<TControl, TDataType> setValueToControl
  37. , Func<TControl, TDataType> getValueFromControl
  38. , TValidation validation) where TControl : WebControl, new()
  39. where TValidation : DymWebFormValidation
  40. {
  41. if (false == ms_dicMapping.ContainsKey(typeof(TDataType)))
  42. {
  43. ms_dicMapping[typeof(TDataType)] = new Dictionary<Type, DymWebFormControlMappnig>();
  44. }
  45. ms_dicMapping[typeof(TDataType)][typeof(TValidation)] = new DymWebFormControlMappnig(
  46. typeof(TControl)
  47. , init == null ? null : new Action<object>(c => init((TControl)c, validation))
  48. , (c, d) => setValueToControl((TControl)c, (TDataType)d)
  49. , c => getValueFromControl((TControl)c)
  50. , validation
  51. );
  52. }
  53. //internal for cctor
  54. private static void Bdts<TD>(TextBox c, TD d)
  55. {
  56. if (d != null)
  57. c.Text = d.ToString();
  58. }
  59. private static T Bdcv<T>(TextBox c) where T : struct
  60. {
  61. return TypeParser<T>.Convert(c.Text);
  62. }
  63. private static T? Bdcvn<T>(TextBox c) where T : struct
  64. {
  65. return TypeParser<T>.ConvertNullable(c.Text);
  66. }
  67. //init bulid-in
  68. static DymWebFormControlMappnig()
  69. {
  70. ms_dicMapping = new Dictionary<Type, Dictionary<Type, DymWebFormControlMappnig>>();
  71. SetControlMapping<string, TextBox, DymWebFormDefaultValidation>(null, Bdts, c => c.Text, null);
  72. SetControlMapping<string, TextBox, DymWebFormEmailValidation>(null, Bdts, c => c.Text, new DymWebFormEmailValidation());
  73. SetControlMapping<string, TextBox, DynWebFormPasswordValidation>((t, v) => t.TextMode = TextBoxMode.Password, Bdts, c => c.Text, new DynWebFormPasswordValidation());
  74. SetControlMapping<string, TextBox, DymWebFormTextAreaValodation>((t, v) =>
  75. {
  76. t.TextMode = TextBoxMode.MultiLine;
  77. t.Columns = v.Cols;
  78. t.Rows = v.Rows;
  79. }, Bdts, c => c.Text, new DymWebFormTextAreaValodation());
  80. SetControlMapping<int, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<int>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  81. SetControlMapping<uint, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<uint>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  82. SetControlMapping<int?, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcvn<int>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  83. SetControlMapping<uint?, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcvn<uint>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  84. SetControlMapping<long, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<long>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
  85. SetControlMapping<float, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<float>, new DymWebFormDefaultValidation(ValidationDataType.Double));
  86. SetControlMapping<double, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<double>, new DymWebFormDefaultValidation(ValidationDataType.Double));
  87. SetControlMapping<double?, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcvn<double>, new DymWebFormDefaultValidation(ValidationDataType.Double));
  88. SetControlMapping<decimal, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<decimal>, new DymWebFormDefaultValidation(ValidationDataType.Double));
  89. SetControlMapping<DateTime, DateTimeTextBox, DwfDateTimeValidation>(null, Bdts, Bdcv<DateTime>, new DwfDateTimeValidation());
  90. SetControlMapping<DateTime?, DateTimeTextBox, DwfDateTimeValidation>(null, Bdts, Bdcvn<DateTime>, new DwfDateTimeValidation());
  91. SetControlMapping<bool, CheckBox, DymWebFormDefaultValidation>(null, (c, d) => c.Checked = d, c => c.Checked, null);
  92. SetControlMapping<Guid, TextBox, DwfGuidValidation>(null, Bdts, c => new Guid(c.Text), new DwfGuidValidation());
  93. SetControlMapping<Guid?, TextBox, DwfGuidValidation>(null, Bdts, c => string.IsNullOrEmpty(c.Text) ? (Guid?)null : new Guid(c.Text), new DwfGuidValidation());
  94. }
  95. //static
  96. internal static DymWebFormControlMappnig GetInst(Type dataType, Type validationType)
  97. {
  98. if (false == ms_dicMapping.ContainsKey(dataType))
  99. throw new ArgumentException("Not registed data type:" + dataType);
  100. if (validationType == null)
  101. return ms_dicMapping[dataType].First();
  102. if (false == ms_dicMapping[dataType].ContainsKey(validationType))
  103. throw new ArgumentException("Not registed validation type of the data type:" + validationType);
  104. return ms_dicMapping[dataType][validationType];
  105. }
  106. }
  107. public abstract class DymWebFormValidation
  108. {
  109. private enum ControlType
  110. {
  111. None,
  112. Basic,
  113. Regexp,
  114. Custom,
  115. }
  116. private ControlType m_ControlType;
  117. //basic
  118. private ValidationDataType m_vdtCompareValidatorDataType;
  119. //cuctom
  120. private ServerValidateEventHandler m_svCustomValidate;
  121. private string m_strClientValidateJs;
  122. //regexp
  123. private string m_regexp;
  124. //ctor
  125. public DymWebFormValidation(ValidationDataType vdt)
  126. {
  127. m_ControlType = ControlType.Basic;
  128. m_vdtCompareValidatorDataType = vdt;
  129. }
  130. public DymWebFormValidation(ServerValidateEventHandler sve, string clientValidateJS = null)
  131. {
  132. m_ControlType = ControlType.Custom;
  133. m_svCustomValidate = sve;
  134. m_strClientValidateJs = clientValidateJS;
  135. }
  136. public DymWebFormValidation(string regexp)
  137. {
  138. m_ControlType = ControlType.Regexp;
  139. m_regexp = regexp;
  140. }
  141. public DymWebFormValidation()
  142. {
  143. m_ControlType = ControlType.None;
  144. }
  145. //get
  146. internal BaseValidator CreateValidator(Control container)
  147. {
  148. switch (m_ControlType)
  149. {
  150. case ControlType.None:
  151. return new CustomValidator() { Enabled = false };
  152. case ControlType.Basic:
  153. return new CompareValidator()
  154. {
  155. Operator = ValidationCompareOperator.DataTypeCheck,
  156. Type = m_vdtCompareValidatorDataType
  157. };
  158. case ControlType.Regexp:
  159. return new RegularExpressionValidator()
  160. {
  161. ValidationExpression = m_regexp,
  162. };
  163. case ControlType.Custom:
  164. var cv = new CustomValidator();
  165. cv.ServerValidate += m_svCustomValidate;
  166. if (string.IsNullOrEmpty(m_strClientValidateJs) == false)
  167. {
  168. cv.ClientValidationFunction = ""
  169. + "ClientValidationFunction"
  170. + "_" + m_strClientValidateJs
  171. .GetHashCode()
  172. .ToBytes()
  173. .ToHexString();
  174. string js =
  175. "function " + cv.ClientValidationFunction + "(source, arguments){"
  176. + m_strClientValidateJs
  177. + "}";
  178. ScriptManager.RegisterClientScriptBlock(container.Page, container.Page.GetType(), cv.ClientValidationFunction, js, true);
  179. }
  180. return cv;
  181. default:
  182. throw new Exception("Unknown Validation ControlType");
  183. }
  184. }
  185. }
  186. public sealed class DwfGuidValidation : DymWebFormValidation
  187. {
  188. private static string clientValidateJS =
  189. "arguments.IsValid="
  190. + "/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i"
  191. + ".test(arguments.Value);";
  192. private static void Validation(object source, ServerValidateEventArgs args)
  193. {
  194. try
  195. {
  196. new Guid(args.Value);
  197. args.IsValid = true;
  198. }
  199. catch (Exception)
  200. {
  201. args.IsValid = false;
  202. }
  203. }
  204. public DwfGuidValidation()
  205. : base(Validation, clientValidateJS)
  206. {
  207. }
  208. }
  209. public sealed class DwfDateTimeValidation : DymWebFormValidation
  210. {
  211. private static string clientValidateJS =
  212. "arguments.IsValid=!isNaN(new Date(arguments.Value).getMilliseconds());";
  213. private static void Validation(object source, ServerValidateEventArgs args)
  214. {
  215. DateTime tmp;
  216. args.IsValid = DateTime.TryParse(args.Value, out tmp);
  217. }
  218. public DwfDateTimeValidation()
  219. : base(Validation, clientValidateJS)
  220. {
  221. }
  222. }
  223. public sealed class DymWebFormDefaultValidation : DymWebFormValidation
  224. {
  225. public DymWebFormDefaultValidation(ValidationDataType vdt)
  226. : base(vdt)
  227. {
  228. }
  229. public DymWebFormDefaultValidation(ServerValidateEventHandler sve, string clientValidateJS = null)
  230. : base(sve, clientValidateJS)
  231. {
  232. }
  233. }
  234. public sealed class DymWebFormEmailValidation : DymWebFormValidation
  235. {
  236. public DymWebFormEmailValidation()
  237. : base(@"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$")
  238. {
  239. }
  240. }
  241. public sealed class DynWebFormPasswordValidation : DymWebFormValidation
  242. {
  243. }
  244. public sealed class DymWebFormTextAreaValodation : DymWebFormValidation
  245. {
  246. public int Cols { get; private set; }
  247. public int Rows { get; private set; }
  248. public DymWebFormTextAreaValodation(int cols = 30, int rows = 5)
  249. {
  250. Cols = cols;
  251. Rows = rows;
  252. }
  253. }
  254. }