Answered by:
Custom Validation CustomValidator OnServerValidate Event not fire

Question
-
User2070100822 posted
Hi...
I created Custom CustomValidator control in order to set dynamic errorMassege base on user rank, and i am haveing problem with it because the OnServerValidate event not fire, everything else is working great. the code for this control:
1 namespace CustomValidator 2 { 3 [DefaultProperty("LiteralID")] 4 [ToolboxData("<{0}:RmCustomValidator runat=server></{0}:RmCustomValidator>")] 5 public class RmCustomValidator : CustomValidator 6 { 7 private CustomValidator req; 8 private string _currPage = ""; 9 private string _sPath = ""; 10 private string _extention = ""; 11 private string _langId = ""; 12 private string _text = ""; 13 private string _literalID = ""; 14 15 private string CurrentPage 16 { 17 get { return _currPage; } 18 set { _currPage = value; } 19 } 20 private string SPath 21 { 22 get { return _sPath; } 23 set { _sPath = value; } 24 } 25 private string Extention 26 { 27 get { return _extention; } 28 set { _extention = value; } 29 } 30 private string LangID 31 { 32 get { return _langId; } 33 set { _langId = value; } 34 } 35 private string ErrorText 36 { 37 get { return _text; } 38 set { _text = value; } 39 } 40 41 public string ClientScript = "true"; 42 43 [Bindable(true)] 44 [Category("Behavior")] 45 [DefaultValue("")] 46 [Localizable(true)] 47 [Description("The id of the literal that holds the error message.")] 48 public string LiteralID 49 { 50 get 51 { 52 return _literalID; 53 } 54 set 55 { 56 _literalID = value; 57 } 58 } 59 60 protected override void OnInit(EventArgs e) 61 { 62 if ( this.DesignMode ) 63 return; 64 65 req = new CustomValidator(); 66 67 req.ErrorMessage = string from db// Here i have the code that bring the error messege from dataBase, base on user rank. 68 69 if ( this.Display != ValidatorDisplay.Dynamic ) 70 { 71 req.Display = ValidatorDisplay.None; 72 } 73 74 req.EnableClientScript = this.EnableClientScript; 75 req.ClientValidationFunction = this.ClientValidationFunction; 76 req.ControlToValidate = this.ControlToValidate; 77 Controls.Add(req); 78 } 79 80 protected override void Render(HtmlTextWriter output) 81 { 82 try 83 { 84 req.RenderControl(output); 85 } 86 87 catch ( Exception ex ) 88 { 89 // Something bad happened. Let's tell the user what that was. 90 output.Write("Error building CustomValidator control:<br>"); 91 output.Write(ex.Message); 92 } 93 } 94 } 95 }
I also tried to add this to the control:
protected override bool EvaluateIsValid() { string s = GetControlValidationValue( this.ControlToValidate ); return this.OnServerValidate( s ); }
or:
private static readonly object EventServerValidate; protected override bool OnServerValidate(string value) { ServerValidateEventHandler handler = (ServerValidateEventHandler)base.Events[EventServerValidate]; ServerValidateEventArgs args = new ServerValidateEventArgs(value, true); if (handler != null) { handler(this, args); return args.IsValid; } return true; }
What do i missing here?
Thanks....
Thursday, May 1, 2008 9:54 AM
Answers
-
User2070100822 posted
I solve the problem: this is the code:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Text; 5 using System.Web; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Security.Permissions; 9 using RM.BL.CacheManager; 10 11 namespace RM.Controls.WebControls 12 { 13 [ToolboxData("<{0}:RmCustomValidator runat=\"server\" LiteralID=\"\" ErrorMessage=\"CustomValidator\"></{0}:RmCustomValidator>"), DefaultEvent("ServerValidate")] 14 public class RmCustomValidator : BaseValidator 15 { 16 private string _LiteralID; 17 18 protected override void OnInit(EventArgs e) 19 { 20 SetLiteralID(); 21 base.OnInit(e); 22 } 23 24 private void SetLiteralID() 25 { 26 if ( _LiteralID == null ) 27 { 28 throw new Exception("You must indicate the id of the literal control."); 29 } 30 else 31 { 32 string valueFromDB = //string from db, Here i have the code that bring the error messege from dataBase, base on user rank. 33 34 if ( !string.IsNullOrEmpty(valueFromDB) ) 35 { 36 this.ViewState["LiteralID"] = valueFromDB; 37 this.ViewState["ErrorMessage"] = valueFromDB; 38 } 39 } 40 } 41 42 // Fields 43 private static readonly object EventServerValidate = new object(); 44 45 // Events 46 public event ServerValidateEventHandler ServerValidate 47 { 48 add 49 { 50 base.Events.AddHandler(EventServerValidate, value); 51 } 52 remove 53 { 54 base.Events.RemoveHandler(EventServerValidate, value); 55 } 56 } 57 58 // Methods 59 protected override bool ControlPropertiesValid() 60 { 61 string controlToValidate = base.ControlToValidate; 62 if ( controlToValidate.Length > 0 ) 63 { 64 base.CheckControlValidationProperty(controlToValidate, "ControlToValidate"); 65 } 66 return true; 67 } 68 69 protected override bool EvaluateIsValid() 70 { 71 string controlValidationValue = string.Empty; 72 string controlToValidate = base.ControlToValidate; 73 if ( controlToValidate.Length > 0 ) 74 { 75 controlValidationValue = base.GetControlValidationValue(controlToValidate); 76 if ( ( ( controlValidationValue == null ) || ( controlValidationValue.Trim().Length == 0 ) ) && !this.ValidateEmptyText ) 77 { 78 return true; 79 } 80 } 81 return this.OnServerValidate(controlValidationValue); 82 } 83 84 protected virtual bool OnServerValidate(string value) 85 { 86 ServerValidateEventHandler handler = (ServerValidateEventHandler)base.Events[EventServerValidate]; 87 ServerValidateEventArgs args = new ServerValidateEventArgs(value, true); 88 if ( handler != null ) 89 { 90 handler(this, args); 91 return args.IsValid; 92 } 93 return true; 94 } 95 96 // Properties 97 public string LiteralID 98 { 99 get 100 { 101 object obj2 = this.ViewState["LiteralID"]; 102 if ( obj2 != null ) 103 { 104 return (string)obj2; 105 } 106 return string.Empty; 107 } 108 set 109 { 110 _LiteralID = value; 111 } 112 } 113 114 public new string ErrorMessage 115 { 116 get 117 { 118 object obj2 = this.ViewState["ErrorMessage"]; 119 if ( obj2 != null ) 120 { 121 return (string)obj2; 122 } 123 return string.Empty; 124 } 125 set 126 { 127 if ( !string.IsNullOrEmpty(LiteralID) ) 128 { 129 this.ViewState["ErrorMessage"] = LiteralID; 130 } 131 else 132 { 133 this.ViewState["ErrorMessage"] = value; 134 } 135 } 136 } 137 138 public string ClientValidationFunction 139 { 140 get 141 { 142 object obj2 = this.ViewState["ClientValidationFunction"]; 143 if ( obj2 != null ) 144 { 145 return (string)obj2; 146 } 147 return string.Empty; 148 } 149 set 150 { 151 this.ViewState["ClientValidationFunction"] = value; 152 } 153 } 154 155 protected override void AddAttributesToRender(HtmlTextWriter writer) 156 { 157 base.AddAttributesToRender(writer); 158 if ( base.RenderUplevel ) 159 { 160 string clientID = this.ClientID; 161 AddExpandoAttribute(writer, clientID, "evaluationfunction", "CustomValidatorEvaluateIsValid", false); 162 if ( this.ClientValidationFunction.Length > 0 ) 163 { 164 AddExpandoAttribute(writer, clientID, "clientvalidationfunction", this.ClientValidationFunction); 165 if ( this.ValidateEmptyText ) 166 { 167 AddExpandoAttribute(writer, clientID, "validateemptytext", "true", false); 168 } 169 } 170 } 171 } 172 173 private void AddExpandoAttribute(HtmlTextWriter writer, string controlId, string attributeName, string attributeValue) 174 { 175 this.AddExpandoAttribute(writer, controlId, attributeName, attributeValue, true); 176 } 177 178 private void AddExpandoAttribute(HtmlTextWriter writer, string controlId, string attributeName, string attributeValue, bool encode) 179 { 180 AddExpandoAttribute(this, writer, controlId, attributeName, attributeValue, encode); 181 } 182 183 private static void AddExpandoAttribute(Control control, HtmlTextWriter writer, string controlId, string attributeName, string attributeValue, bool encode) 184 { 185 if ( writer != null ) 186 { 187 writer.AddAttribute(attributeName, attributeValue, encode); 188 } 189 else 190 { 191 Page page = control.Page; 192 page.ClientScript.RegisterExpandoAttribute(controlId, attributeName, attributeValue, encode); 193 } 194 } 195 196 public bool ValidateEmptyText 197 { 198 get 199 { 200 object obj2 = this.ViewState["ValidateEmptyText"]; 201 return ( ( obj2 != null ) && ( (bool)obj2 ) ); 202 } 203 set 204 { 205 this.ViewState["ValidateEmptyText"] = value; 206 } 207 } 208 } 209 }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 20, 2008 3:38 AM