CustomValidator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // System.Web.UI.WebControls.CustomValidator.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Web;
  33. using System.Web.UI;
  34. using System.ComponentModel;
  35. namespace System.Web.UI.WebControls
  36. {
  37. [DefaultEvent("ServerValidate")]
  38. [ToolboxData("<{0}:CustomValidator runat=\"server\""
  39. + "ErrorMessage=\"CustomValidator\">"
  40. + "</{0}:CustomValidator>")]
  41. public class CustomValidator : BaseValidator
  42. {
  43. private static readonly object ServerValidateEvent = new object();
  44. public CustomValidator()
  45. {
  46. }
  47. [DefaultValue (""), WebCategory ("Behavior")]
  48. [WebSysDescription ("A client script that performs the validation.")]
  49. public string ClientValidationFunction
  50. {
  51. get
  52. {
  53. object o = ViewState["ClientValidationFunction"];
  54. if(o != null)
  55. {
  56. return (string)o;
  57. }
  58. return String.Empty;
  59. }
  60. set
  61. {
  62. ViewState["ClientValidationFunction"] = value;
  63. }
  64. }
  65. [WebSysDescription ("Raised for validation on the server.")]
  66. public event ServerValidateEventHandler ServerValidate
  67. {
  68. add
  69. {
  70. Events.AddHandler(ServerValidateEvent, value);
  71. }
  72. remove
  73. {
  74. Events.RemoveHandler(ServerValidateEvent, value);
  75. }
  76. }
  77. protected override void AddAttributesToRender(HtmlTextWriter writer)
  78. {
  79. base.AddAttributesToRender(writer);
  80. if(RenderUplevel)
  81. {
  82. writer.AddAttribute("evaluationfunction", "CustomValidatorEvaluateIsValid");
  83. if(ClientValidationFunction.Length > 0)
  84. {
  85. writer.AddAttribute("clientvalidationfunction", ClientValidationFunction);
  86. }
  87. }
  88. }
  89. protected override bool ControlPropertiesValid()
  90. {
  91. if(ControlToValidate.Length > 0)
  92. {
  93. CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
  94. }
  95. return true;
  96. }
  97. protected virtual bool OnServerValidate(string value)
  98. {
  99. if(Events != null)
  100. {
  101. ServerValidateEventHandler sveh = (ServerValidateEventHandler)(Events[ServerValidateEvent]);
  102. if(sveh != null)
  103. {
  104. ServerValidateEventArgs args = new ServerValidateEventArgs(value, true);
  105. sveh(this, args);
  106. return args.IsValid;
  107. }
  108. }
  109. return true;
  110. }
  111. protected override bool EvaluateIsValid()
  112. {
  113. string ctrl = ControlToValidate;
  114. if(ctrl.Length > 0)
  115. {
  116. ctrl = GetControlValidationValue(ctrl);
  117. if(ctrl== null || ctrl.Length == 0)
  118. {
  119. return true;
  120. }
  121. }
  122. return OnServerValidate(ctrl);
  123. }
  124. }
  125. }