123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace DymWebForm.Aspx
- {
- public class DymWebFormControlMappnig
- {
- private readonly Type _typControl;
- private readonly Action<object> _delInitControl;
- public Action<object, object> ControlSetValue { get; set; }
- public Func<object, object> ControlGetValue { get; private set; }
- public DymWebFormValidation Validation { get; set; }
- //
- internal WebControl CreateControl()
- {
- var c = (WebControl)Activator.CreateInstance(_typControl);
- if (_delInitControl != null)
- _delInitControl(c);
- return c;
- }
- //
- private DymWebFormControlMappnig(Type controlType, Action<object> controlInit, Action<object, object> controlSetValue, Func<object, object> controlGetValue, DymWebFormValidation validation)
- {
- this._typControl = controlType;
- this._delInitControl = controlInit;
- this.ControlSetValue = controlSetValue;
- this.ControlGetValue = controlGetValue;
- this.Validation = validation;
- }
- //
- private static Dictionary<Type, Dictionary<Type, DymWebFormControlMappnig>> ms_dicMapping;
- public static void SetControlMapping<TDataType, TControl, TValidation>(
- Action<TControl, TValidation> init
- , Action<TControl, TDataType> setValueToControl
- , Func<TControl, TDataType> getValueFromControl
- , TValidation validation) where TControl : WebControl, new()
- where TValidation : DymWebFormValidation
- {
- if (false == ms_dicMapping.ContainsKey(typeof(TDataType)))
- {
- ms_dicMapping[typeof(TDataType)] = new Dictionary<Type, DymWebFormControlMappnig>();
- }
- ms_dicMapping[typeof(TDataType)][typeof(TValidation)] = new DymWebFormControlMappnig(
- typeof(TControl)
- , init == null ? null : new Action<object>(c => init((TControl)c, validation))
- , (c, d) => setValueToControl((TControl)c, (TDataType)d)
- , c => getValueFromControl((TControl)c)
- , validation
- );
- }
- //internal for cctor
- private static void Bdts<TD>(TextBox c, TD d)
- {
- if (d != null)
- c.Text = d.ToString();
- }
- private static T Bdcv<T>(TextBox c) where T : struct
- {
- return TypeParser<T>.Convert(c.Text);
- }
- private static T? Bdcvn<T>(TextBox c) where T : struct
- {
- return TypeParser<T>.ConvertNullable(c.Text);
- }
- //init bulid-in
- static DymWebFormControlMappnig()
- {
- ms_dicMapping = new Dictionary<Type, Dictionary<Type, DymWebFormControlMappnig>>();
- SetControlMapping<string, TextBox, DymWebFormDefaultValidation>(null, Bdts, c => c.Text, null);
- SetControlMapping<string, TextBox, DymWebFormEmailValidation>(null, Bdts, c => c.Text, new DymWebFormEmailValidation());
- SetControlMapping<string, TextBox, DynWebFormPasswordValidation>((t, v) => t.TextMode = TextBoxMode.Password, Bdts, c => c.Text, new DynWebFormPasswordValidation());
- SetControlMapping<string, TextBox, DymWebFormTextAreaValodation>((t, v) =>
- {
- t.TextMode = TextBoxMode.MultiLine;
- t.Columns = v.Cols;
- t.Rows = v.Rows;
- }, Bdts, c => c.Text, new DymWebFormTextAreaValodation());
- SetControlMapping<int, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<int>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
- SetControlMapping<uint, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<uint>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
- SetControlMapping<int?, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcvn<int>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
- SetControlMapping<uint?, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcvn<uint>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
- SetControlMapping<long, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<long>, new DymWebFormDefaultValidation(ValidationDataType.Integer));
- SetControlMapping<float, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<float>, new DymWebFormDefaultValidation(ValidationDataType.Double));
- SetControlMapping<double, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<double>, new DymWebFormDefaultValidation(ValidationDataType.Double));
- SetControlMapping<double?, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcvn<double>, new DymWebFormDefaultValidation(ValidationDataType.Double));
- SetControlMapping<decimal, TextBox, DymWebFormDefaultValidation>(null, Bdts, Bdcv<decimal>, new DymWebFormDefaultValidation(ValidationDataType.Double));
- SetControlMapping<DateTime, DateTimeTextBox, DwfDateTimeValidation>(null, Bdts, Bdcv<DateTime>, new DwfDateTimeValidation());
- SetControlMapping<DateTime?, DateTimeTextBox, DwfDateTimeValidation>(null, Bdts, Bdcvn<DateTime>, new DwfDateTimeValidation());
- SetControlMapping<bool, CheckBox, DymWebFormDefaultValidation>(null, (c, d) => c.Checked = d, c => c.Checked, null);
- SetControlMapping<Guid, TextBox, DwfGuidValidation>(null, Bdts, c => new Guid(c.Text), new DwfGuidValidation());
- SetControlMapping<Guid?, TextBox, DwfGuidValidation>(null, Bdts, c => string.IsNullOrEmpty(c.Text) ? (Guid?)null : new Guid(c.Text), new DwfGuidValidation());
- }
- //static
- internal static DymWebFormControlMappnig GetInst(Type dataType, Type validationType)
- {
- if (false == ms_dicMapping.ContainsKey(dataType))
- throw new ArgumentException("Not registed data type:" + dataType);
- if (validationType == null)
- return ms_dicMapping[dataType].First();
- if (false == ms_dicMapping[dataType].ContainsKey(validationType))
- throw new ArgumentException("Not registed validation type of the data type:" + validationType);
- return ms_dicMapping[dataType][validationType];
- }
- }
- public abstract class DymWebFormValidation
- {
- private enum ControlType
- {
- None,
- Basic,
- Regexp,
- Custom,
- }
- private ControlType m_ControlType;
- //basic
- private ValidationDataType m_vdtCompareValidatorDataType;
- //cuctom
- private ServerValidateEventHandler m_svCustomValidate;
- private string m_strClientValidateJs;
- //regexp
- private string m_regexp;
- //ctor
- public DymWebFormValidation(ValidationDataType vdt)
- {
- m_ControlType = ControlType.Basic;
- m_vdtCompareValidatorDataType = vdt;
- }
- public DymWebFormValidation(ServerValidateEventHandler sve, string clientValidateJS = null)
- {
- m_ControlType = ControlType.Custom;
- m_svCustomValidate = sve;
- m_strClientValidateJs = clientValidateJS;
- }
- public DymWebFormValidation(string regexp)
- {
- m_ControlType = ControlType.Regexp;
- m_regexp = regexp;
- }
- public DymWebFormValidation()
- {
- m_ControlType = ControlType.None;
- }
- //get
- internal BaseValidator CreateValidator(Control container)
- {
- switch (m_ControlType)
- {
- case ControlType.None:
- return new CustomValidator() { Enabled = false };
- case ControlType.Basic:
- return new CompareValidator()
- {
- Operator = ValidationCompareOperator.DataTypeCheck,
- Type = m_vdtCompareValidatorDataType
- };
- case ControlType.Regexp:
- return new RegularExpressionValidator()
- {
- ValidationExpression = m_regexp,
- };
- case ControlType.Custom:
- var cv = new CustomValidator();
- cv.ServerValidate += m_svCustomValidate;
- if (string.IsNullOrEmpty(m_strClientValidateJs) == false)
- {
- cv.ClientValidationFunction = ""
- + "ClientValidationFunction"
- + "_" + m_strClientValidateJs
- .GetHashCode()
- .ToBytes()
- .ToHexString();
- string js =
- "function " + cv.ClientValidationFunction + "(source, arguments){"
- + m_strClientValidateJs
- + "}";
- ScriptManager.RegisterClientScriptBlock(container.Page, container.Page.GetType(), cv.ClientValidationFunction, js, true);
- }
- return cv;
- default:
- throw new Exception("Unknown Validation ControlType");
- }
- }
- }
- public sealed class DwfGuidValidation : DymWebFormValidation
- {
- private static string clientValidateJS =
- "arguments.IsValid="
- + "/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i"
- + ".test(arguments.Value);";
- private static void Validation(object source, ServerValidateEventArgs args)
- {
- try
- {
- new Guid(args.Value);
- args.IsValid = true;
- }
- catch (Exception)
- {
- args.IsValid = false;
- }
- }
- public DwfGuidValidation()
- : base(Validation, clientValidateJS)
- {
- }
- }
- public sealed class DwfDateTimeValidation : DymWebFormValidation
- {
- private static string clientValidateJS =
- "arguments.IsValid=!isNaN(new Date(arguments.Value).getMilliseconds());";
- private static void Validation(object source, ServerValidateEventArgs args)
- {
- DateTime tmp;
- args.IsValid = DateTime.TryParse(args.Value, out tmp);
- }
- public DwfDateTimeValidation()
- : base(Validation, clientValidateJS)
- {
- }
- }
- public sealed class DymWebFormDefaultValidation : DymWebFormValidation
- {
- public DymWebFormDefaultValidation(ValidationDataType vdt)
- : base(vdt)
- {
- }
- public DymWebFormDefaultValidation(ServerValidateEventHandler sve, string clientValidateJS = null)
- : base(sve, clientValidateJS)
- {
- }
- }
- public sealed class DymWebFormEmailValidation : DymWebFormValidation
- {
- public DymWebFormEmailValidation()
- : base(@"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$")
- {
- }
- }
- public sealed class DynWebFormPasswordValidation : DymWebFormValidation
- {
- }
- public sealed class DymWebFormTextAreaValodation : DymWebFormValidation
- {
- public int Cols { get; private set; }
- public int Rows { get; private set; }
- public DymWebFormTextAreaValodation(int cols = 30, int rows = 5)
- {
- Cols = cols;
- Rows = rows;
- }
- }
- }
|