RangeValidator.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // System.Web.UI.WebControls.RangeValidator.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.ComponentModel;
  33. using System.Web;
  34. using System.Web.UI;
  35. using System.Xml;
  36. namespace System.Web.UI.WebControls
  37. {
  38. [ToolboxData("<{0}:RangeValidator runat=\"server\" "
  39. + "ErrorMessage=\"RangeValidator\"></{0}:RangeValidator>")]
  40. public class RangeValidator : BaseCompareValidator
  41. {
  42. public RangeValidator(): base()
  43. {
  44. }
  45. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  46. [WebSysDescription ("The maximum value that the validated control can be assigned.")]
  47. public string MaximumValue
  48. {
  49. get
  50. {
  51. object o = ViewState["MaximumValue"];
  52. if(o != null)
  53. {
  54. return (string)o;
  55. }
  56. return String.Empty;
  57. }
  58. set
  59. {
  60. ViewState["MaximumValue"] = value;
  61. }
  62. }
  63. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  64. [WebSysDescription ("The minimum value that the validated control can be assigned.")]
  65. public string MinimumValue
  66. {
  67. get
  68. {
  69. object o = ViewState["MinimumValue"];
  70. if(o != null)
  71. {
  72. return (string)o;
  73. }
  74. return String.Empty;
  75. }
  76. set
  77. {
  78. ViewState["MinimumValue"] = value;
  79. }
  80. }
  81. protected override void AddAttributesToRender(HtmlTextWriter writer)
  82. {
  83. base.AddAttributesToRender(writer);
  84. if(base.RenderUplevel)
  85. {
  86. writer.AddAttribute("evaluationfunction", "RangeValidatorEvaluateIsValid");
  87. writer.AddAttribute("maximumvalue", MaximumValue);
  88. writer.AddAttribute("minimumvalue", MinimumValue);
  89. }
  90. }
  91. protected override bool ControlPropertiesValid()
  92. {
  93. string max = MaximumValue;
  94. if(!CanConvert(max, Type))
  95. {
  96. string[] fmt = new string[4];
  97. fmt[0] = max;
  98. fmt[1] = "MaximumValue";
  99. fmt[2] = ID;
  100. fmt[3] = PropertyConverter.EnumToString(typeof(ValidationDataType), Type);
  101. throw new HttpException(HttpRuntime.FormatResourceString("Validator_value_bad_type", fmt));
  102. }
  103. string min = MinimumValue;
  104. if(!CanConvert(min, Type))
  105. {
  106. string[] fmt = new string[4];
  107. fmt[0] = min;
  108. fmt[1] = "MinimumValue";
  109. fmt[2] = ID;
  110. fmt[3] = PropertyConverter.EnumToString(typeof(ValidationDataType), Type);
  111. throw new HttpException(HttpRuntime.FormatResourceString("Validator_value_bad_type", fmt));
  112. }
  113. if(Compare(min,max, ValidationCompareOperator.GreaterThan, Type))
  114. {
  115. string[] fmt = new string[3];
  116. fmt[0] = min;
  117. fmt[1] = max;
  118. fmt[2] = PropertyConverter.EnumToString(typeof(ValidationDataType), Type);
  119. throw new HttpException(HttpRuntime.FormatResourceString("Validator_range_overalap", fmt));
  120. }
  121. return base.ControlPropertiesValid();
  122. }
  123. protected override bool EvaluateIsValid()
  124. {
  125. string ctrl = GetControlValidationValue(ControlToValidate);
  126. if(ctrl == null || ctrl.Trim().Length == 0)
  127. {
  128. return true;
  129. }
  130. bool retVal = Compare(ctrl, MinimumValue, ValidationCompareOperator.GreaterThanEqual,
  131. this.Type);
  132. if(retVal)
  133. {
  134. retVal = Compare(ctrl, MaximumValue, ValidationCompareOperator.LessThanEqual,
  135. this.Type);
  136. }
  137. return retVal;
  138. }
  139. }
  140. }