CustomValidator.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. namespace System.Web.UI.WebControls
  17. {
  18. public class CustomValidator : BaseValidator
  19. {
  20. private static readonly object ServerValidateEvent = new object();
  21. public CustomValidator()
  22. {
  23. }
  24. public string ClientValidationFunction
  25. {
  26. get
  27. {
  28. object o = ViewState["ClientValidationFunction"];
  29. if(o != null)
  30. {
  31. return (string)o;
  32. }
  33. return String.Empty;
  34. }
  35. set
  36. {
  37. ViewState["ClientValidationFunction"] = value;
  38. }
  39. }
  40. public event ServerValidateEventHandler ServerValidate
  41. {
  42. add
  43. {
  44. Events.AddHandler(ServerValidateEvent, value);
  45. }
  46. remove
  47. {
  48. Events.RemoveHandler(ServerValidateEvent, value);
  49. }
  50. }
  51. protected override void AddAttributesToRender(HtmlTextWriter writer)
  52. {
  53. base.AddAttributesToRender(writer);
  54. if(RenderUplevel)
  55. {
  56. writer.AddAttribute("evaluationfunction", "CustomValidatorEvaluateIsValid");
  57. if(ClientValidationFunction.Length > 0)
  58. {
  59. writer.AddAttribute("clientvalidationfunction", ClientValidationFunction);
  60. }
  61. }
  62. }
  63. protected override bool ControlPropertiesValid()
  64. {
  65. if(ControlToValidate.Length > 0)
  66. {
  67. CheckControlValidationProperty(ControlToValidate, "ControlToValidate");
  68. }
  69. }
  70. protected virtual bool OnServerValidate(string value)
  71. {
  72. if(Events != null)
  73. {
  74. ServerValidateEventHandler sveh = (ServerValidateEventHandler)(Events[ServerValidateEvent]);
  75. if(sveh != null)
  76. {
  77. ServerValidateEventArgs args = new ServerValidateEventArgs(value, true));
  78. sveh(this, args);
  79. return args.IsValid;
  80. }
  81. }
  82. return true;
  83. }
  84. }
  85. }