From: Lofty <Lo...@ez...> - 2006-03-23 20:12:27
|
if you want to programatically add validators to a page via a callback. Anthem needs a few tweaks, I whiped this up today in work (I see someone else posted up anthemised validators today to .. sweet :) but these tweaks are specifically for adding validators programatically via a callback, which cant be done even via an anthemised validator control, ya need these mods). I'm hoping that these changes will be used and improved by the community. first off is the javascript, i've added the following methods to the anthem.js file. function Anthem_RemoveOldValidators() { if (typeof(Page_Validators)!="undefined") { var doRemove = -1; for (i=0; i<Page_Validators.length; i++) { doRemove = -1; if (document.getElementById(Page_Validators[i].controltovalidate) == null) doRemove = i; if (document.getElementById(Page_Validators[i].id) == null && doRemove == -1) doRemove = i; for (j=i + 1; j<Page_Validators.length; j++) { if (Page_Validators[i].id == Page_Validators[j].id) doRemove = i; } if (doRemove > -1) { Page_Validators.splice(doRemove,1); i--; } } } } function Anthem_RemoveValidator(validator) { if (typeof(Page_Validators)!="undefined") { for (i=0; i<Page_Validators.length; i++) { if (Page_Validators[i].id == validator) { Page_Validators.splice(i,1); i--; } } } } function Anthem_AddNewValidator(validator) { var newValidator = document.getElementById(validator); if (typeof(Page_Validators)!="undefined") { if (typeof(newValidator.evaluationfunction) == "string") eval("newValidator.evaluationfunction = " + newValidator.evaluationfunction + ";"); Page_Validators.splice(Page_Validators.length + 1,0,newValidator); } } what this does is allows you to add a new validator to the Page_Validators array, remove obsolete validators, ie. if after a callback you hide a control that was prviously validated. the validator will still fire even though the control no longer exists in the page. the use of these are simple as. just call them as required in your server side methods. Anthem.Manager.AddScriptForClientSideEval("Anthem_AddNewValidator('Validator Id Goes Here');"); Anthem.Manager.AddScriptForClientSideEval("Anthem_RemoveOldValidators();"); preferrably call the Anthem_RemoveOldValidators() function once at the end of the serveside methods. now if you do not have any validators on your page at load time the webuivalidation.js script, the page_validators array and the various checks and osubmit guff wont be included.. and never will be if your only doing callbacks. so i've included a control, validatorsupport. just plonk one of those onto your aspx page. and it will make sure that everything that needs to be included will be. dont worry though. even though this control inherits from basevalidator and appears to render a textbox to validate. nothing will ever get rendered to your page, so dont worry about having these controls apearing and messing up your layout, they wont even be included in the html source at all. but the control to validate and inherting from basevalidaotr was required to get .net to render all the required script snippets. I did get this working without inheriting from basevalidators overriding various methods and submitting the javascript myself.. but it was messy and didnt support 2.0 yet , so i thought better to let .net handle all that for us. this validator support control just place it anywhere on ya aspx page. <Anthem:ValidatorSupport id="ValidatorSupport1" runat="server"></Anthem:ValidatorSupport> please note that if you do already have .net validators included on your page when it is first loaded then you do not need the validator support class. thats only required if you have no validators initailly and then add some later via a callback. there is a couple of issues here. 1. i didnt have time to add in support for the summaries javascript array, (will be very easy though). 2. like all dynamic controls they have no placeholder rendered for them so you need to include them into an anthemised control like a panel or placeholder to have them rendered. to demonstrate adding a validator dynamically. RequiredFieldValidator rfv = new RequiredFieldValidator(); rfv.ControlToValidate = "txtbxThree"; rfv.ID = "validatorOne"; rfv.EnableClientScript = true; rfv.Display = ValidatorDisplay.None; rfv.ErrorMessage = "fill in the textbox!"; Panel1.Controls.Add(rfv); Panel1.Update(); CitiGroup.Anthem.Manager.AddScriptForClientSideEval("Anthem_AddNewValidator('validatorOne');"); CitiGroup.Anthem.Manager.AddScriptForClientSideEval("Anthem_RemoveOldValidators();"); here is the validatorsupport class using System; using System.Web.UI; using ASP = System.Web.UI.WebControls; using System.ComponentModel; namespace Anthem { public class ValidatorSupport : ASP.BaseValidator, IAnthemControl { private const string parentTagName = "span"; protected override bool EvaluateIsValid() { return true; } #region Common Anthem control code (DO NOT EDIT!) protected override void OnLoad(EventArgs e) { base.OnLoad(e); Manager.Register(this); this.EnableClientScript = true; this.Visible = false; TextBox txt = new TextBox(); txt.ID = this.ClientID + "txtbxOne"; this.Controls.Add(txt); this.ControlToValidate = this.ClientID + "txtbxOne"; this.Page.RegisterArrayDeclaration("Page_Validators", ""); } #if V2 public override void RenderControl(HtmlTextWriter writer) { base.Visible = true; base.RenderControl(writer); } #endif protected override void Render(HtmlTextWriter writer) { this.Style.Add("display", "none"); this.Style.Add("visibility", "hidden"); #if !V2 bool DesignMode = this.Context == null; #endif if (!DesignMode) { // parentTagName must be defined as a private const string field in this class. Manager.WriteBeginControlMarker(writer, parentTagName, this); } if (Visible) { base.Render(writer); } if (!DesignMode) { Manager.WriteEndControlMarker(writer, parentTagName, this); } } public override bool Visible { get { return Manager.GetControlVisible(ViewState); } set { Manager.SetControlVisible(ViewState, value); } } private bool _updateAfterCallBack = false; [DefaultValue(false)] public bool UpdateAfterCallBack { get { return _updateAfterCallBack; } set { _updateAfterCallBack = value; } } #endregion } } |