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 _delInitControl; public Action ControlSetValue { get; set; } public Func 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 controlInit, Action controlSetValue, Func controlGetValue, DymWebFormValidation validation) { this._typControl = controlType; this._delInitControl = controlInit; this.ControlSetValue = controlSetValue; this.ControlGetValue = controlGetValue; this.Validation = validation; } // private static Dictionary> ms_dicMapping; public static void SetControlMapping( Action init , Action setValueToControl , Func getValueFromControl , TValidation validation) where TControl : WebControl, new() where TValidation : DymWebFormValidation { if (false == ms_dicMapping.ContainsKey(typeof(TDataType))) { ms_dicMapping[typeof(TDataType)] = new Dictionary(); } ms_dicMapping[typeof(TDataType)][typeof(TValidation)] = new DymWebFormControlMappnig( typeof(TControl) , init == null ? null : new Action(c => init((TControl)c, validation)) , (c, d) => setValueToControl((TControl)c, (TDataType)d) , c => getValueFromControl((TControl)c) , validation ); } //internal for cctor private static void Bdts(TextBox c, TD d) { if (d != null) c.Text = d.ToString(); } private static T Bdcv(TextBox c) where T : struct { return TypeParser.Convert(c.Text); } private static T? Bdcvn(TextBox c) where T : struct { return TypeParser.ConvertNullable(c.Text); } //init bulid-in static DymWebFormControlMappnig() { ms_dicMapping = new Dictionary>(); SetControlMapping(null, Bdts, c => c.Text, null); SetControlMapping(null, Bdts, c => c.Text, new DymWebFormEmailValidation()); SetControlMapping((t, v) => t.TextMode = TextBoxMode.Password, Bdts, c => c.Text, new DynWebFormPasswordValidation()); SetControlMapping((t, v) => { t.TextMode = TextBoxMode.MultiLine; t.Columns = v.Cols; t.Rows = v.Rows; }, Bdts, c => c.Text, new DymWebFormTextAreaValodation()); SetControlMapping(null, Bdts, Bdcv, new DymWebFormDefaultValidation(ValidationDataType.Integer)); SetControlMapping(null, Bdts, Bdcv, new DymWebFormDefaultValidation(ValidationDataType.Integer)); SetControlMapping(null, Bdts, Bdcvn, new DymWebFormDefaultValidation(ValidationDataType.Integer)); SetControlMapping(null, Bdts, Bdcvn, new DymWebFormDefaultValidation(ValidationDataType.Integer)); SetControlMapping(null, Bdts, Bdcv, new DymWebFormDefaultValidation(ValidationDataType.Integer)); SetControlMapping(null, Bdts, Bdcv, new DymWebFormDefaultValidation(ValidationDataType.Double)); SetControlMapping(null, Bdts, Bdcv, new DymWebFormDefaultValidation(ValidationDataType.Double)); SetControlMapping(null, Bdts, Bdcvn, new DymWebFormDefaultValidation(ValidationDataType.Double)); SetControlMapping(null, Bdts, Bdcv, new DymWebFormDefaultValidation(ValidationDataType.Double)); SetControlMapping(null, Bdts, Bdcv, new DwfDateTimeValidation()); SetControlMapping(null, Bdts, Bdcvn, new DwfDateTimeValidation()); SetControlMapping(null, (c, d) => c.Checked = d, c => c.Checked, null); SetControlMapping(null, Bdts, c => new Guid(c.Text), new DwfGuidValidation()); SetControlMapping(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; } } }