CustomValidator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: CustomValidator
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2002)
  12. */
  13. using System;
  14. using System.Web;
  15. using System.Web.UI;
  16. using System.ComponentModel;
  17. namespace System.Web.UI.WebControls
  18. {
  19. [DefaultEvent("ServerValidate")]
  20. [ToolboxData("<{0}:CustomValidator runat=\"server\""
  21. + "ErrorMessage=\"CustomValidator\">"
  22. + "</{0}:CustomValidator>")]
  23. public class CustomValidator : BaseValidator
  24. {
  25. private static readonly object ServerValidateEvent = new object();
  26. public CustomValidator()
  27. {
  28. }
  29. public string ClientValidationFunction
  30. {
  31. get
  32. {
  33. object o = ViewState["ClientValidationFunction"];
  34. if(o != null)
  35. {
  36. return (string)o;
  37. }
  38. return String.Empty;
  39. }
  40. set
  41. {
  42. ViewState["ClientValidationFunction"] = value;
  43. }
  44. }
  45. public event ServerValidateEventHandler ServerValidate
  46. {
  47. add
  48. {
  49. Events.AddHandler(ServerValidateEvent, value);
  50. }
  51. remove
  52. {
  53. Events.RemoveHandler(ServerValidateEvent, value);
  54. }
  55. }
  56. protected override void AddAttributesToRender(HtmlTextWriter writer)
  57. {
  58. base.AddAttributesToRender(writer);
  59. if(RenderUplevel)
  60. {
  61. writer.AddAttribute("evaluationfunction", "CustomValidatorEvaluateIsValid");
  62. if(ClientValidationFunction.Length > 0)
  63. {
  64. writer.AddAttribute("clientvalidationfunction", ClientValidationFunction);
  65. }
  66. }
  67. }
  68. protected override bool ControlPropertiesValid()
  69. {
  70. if(ControlToValidate.Length > 0)
  71. {
  72. CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
  73. }
  74. return true;
  75. }
  76. protected virtual bool OnServerValidate(string value)
  77. {
  78. if(Events != null)
  79. {
  80. ServerValidateEventHandler sveh = (ServerValidateEventHandler)(Events[ServerValidateEvent]);
  81. if(sveh != null)
  82. {
  83. ServerValidateEventArgs args = new ServerValidateEventArgs(value, true);
  84. sveh(this, args);
  85. return args.IsValid;
  86. }
  87. }
  88. return true;
  89. }
  90. protected override bool EvaluateIsValid()
  91. {
  92. string ctrl = ControlToValidate;
  93. if(ctrl.Length > 0)
  94. {
  95. ctrl = GetControlValidationValue(ctrl);
  96. if(ctrl== null || ctrl.Length == 0)
  97. {
  98. return true;
  99. }
  100. }
  101. return OnServerValidate(ctrl);
  102. }
  103. }
  104. }