public static void OnLoad(BaseValidator validator) { ClientScriptManager cs = validator.Page.ClientScript; IEnhancedFormValidator enhancedFormControl = (IEnhancedFormValidator)validator; if(validator.EnableClientScript == true) { cs.RegisterClientScriptResource(typeof(IEnhancedFormValidator), "RDC.EnhancedForm.EnhancedClientValidation.js"); Control control = validator.Parent.FindControl(validator.ControlToValidate); string controlToAffectID = ""; if (control != null) { if (enhancedFormControl.ControlLabel != null) { WebControl controlToAffect = (WebControl)validator.Parent.FindControl(enhancedFormControl.ControlLabel); if (controlToAffect != null) { controlToAffectID = controlToAffect.ClientID; } } if (!cs.IsClientScriptBlockRegistered(validator.GetType(), String.Format("HighlightValidation_{0}", validator.ClientID))) { /* * Get the underlying original CSS settings for the controls so we can remember * the original CSS state before we change to error CSS classes. */ WebControl controlToValidate = (WebControl)validator.Parent.FindControl(validator.ControlToValidate); WebControl controlLabel = (WebControl)validator.Parent.FindControl(enhancedFormControl.ControlLabel); string controlToAffectIDCssClass = ""; string controlToAffectIDErrorCssClass = ""; if(controlLabel != null) { controlToAffectIDCssClass = controlLabel.CssClass; controlToAffectIDErrorCssClass = enhancedFormControl.ControlLabelErrorCssClass; } cs.RegisterClientScriptBlock(validator.GetType(), String.Format("pageEnhancedValidators{0}", validator.ClientID), String.Format("pageEnhancedValidators.push(new Array('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}'));", validator.ClientID, control.ClientID, controlToValidate.CssClass, enhancedFormControl.ControlErrorCssClass, enhancedFormControl.ErrorClientCallbackFunction, controlToAffectID, controlToAffectIDCssClass, controlToAffectIDErrorCssClass), true); } } } }
/// <summary> /// Adds html5/javascript validation by proxying calls to evaluationfunction /// and calling setCustomValidity client-side. /// </summary> public static void AddHtml5Validation(BaseValidator control) { var clientId = control.ClientID; var scriptKey = control.GetType().Name + "-" + clientId; AddHtmlValidationScript(control.Page.ClientScript); control.Page.ClientScript.RegisterStartupScript(control.GetType(), scriptKey, "ValidatorUtil_AddHtml5Validation('" + clientId + "');", true); }