ValidationSummary.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Peter Bartok ([email protected])
  24. //
  25. //
  26. using System;
  27. using System.Collections;
  28. using System.ComponentModel;
  29. using System.Drawing;
  30. using System.Globalization;
  31. using System.Web;
  32. using System.Web.UI;
  33. using System.Web.UI.WebControls;
  34. namespace System.Web.UI.WebControls {
  35. #if NET_2_0
  36. [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  37. #endif
  38. public class ValidationSummary : WebControl {
  39. #region Public Constructors
  40. public ValidationSummary() : base(HtmlTextWriterTag.Div) {
  41. this.ForeColor = Color.Red;
  42. }
  43. #endregion // Public Constructors
  44. #region Public Instance Properties
  45. #if ONLY_1_1
  46. [Bindable(true)]
  47. #endif
  48. [DefaultValue(ValidationSummaryDisplayMode.BulletList)]
  49. public ValidationSummaryDisplayMode DisplayMode {
  50. get {
  51. object obj;
  52. obj = ViewState["DisplayMode"];
  53. if (obj != null) {
  54. return (ValidationSummaryDisplayMode)obj;
  55. }
  56. return ValidationSummaryDisplayMode.BulletList;
  57. }
  58. set {
  59. ViewState["DisplayMode"] = value;
  60. }
  61. }
  62. [DefaultValue(true)]
  63. #if NET_2_0
  64. [Themeable (false)]
  65. #endif
  66. public bool EnableClientScript {
  67. get {
  68. return ViewState.GetBool("EnableClientScript", true);
  69. }
  70. set {
  71. ViewState["EnableClientScript"] = value;
  72. }
  73. }
  74. [DefaultValue("Color [Red]")]
  75. public override System.Drawing.Color ForeColor {
  76. get {
  77. return base.ForeColor;
  78. }
  79. set {
  80. base.ForeColor = value;
  81. }
  82. }
  83. #if ONLY_1_1
  84. [Bindable(true)]
  85. #endif
  86. [DefaultValue("")]
  87. #if NET_2_0
  88. [Localizable (true)]
  89. #endif
  90. public string HeaderText {
  91. get {
  92. return ViewState.GetString("HeaderText", string.Empty);
  93. }
  94. set {
  95. ViewState["HeaderText"] = value;
  96. }
  97. }
  98. #if ONLY_1_1
  99. [Bindable(true)]
  100. #endif
  101. [DefaultValue(false)]
  102. public bool ShowMessageBox {
  103. get {
  104. return ViewState.GetBool("ShowMessageBox", false);
  105. }
  106. set {
  107. ViewState["ShowMessageBox"] = value;
  108. }
  109. }
  110. #if ONLY_1_1
  111. [Bindable(true)]
  112. #endif
  113. [DefaultValue(true)]
  114. public bool ShowSummary {
  115. get {
  116. return ViewState.GetBool("ShowSummary", true);
  117. }
  118. set {
  119. ViewState["ShowSummary"] = value;
  120. }
  121. }
  122. #if NET_2_0
  123. [DefaultValue ("")]
  124. [Themeable (false)]
  125. public string ValidationGroup
  126. {
  127. get {
  128. return ViewState.GetString("ValidationGroup", string.Empty);
  129. }
  130. set {
  131. ViewState["ValidationGroup"] = value;
  132. }
  133. }
  134. #endif
  135. #endregion // Public Instance Properties
  136. #region Public Instance Methods
  137. protected override void AddAttributesToRender(HtmlTextWriter writer) {
  138. base.AddAttributesToRender (writer);
  139. if (EnableClientScript && pre_render_called) {
  140. if (HeaderText != "")
  141. writer.AddAttribute ("headertext", HeaderText);
  142. if (ShowMessageBox)
  143. writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
  144. if (ShowSummary == false)
  145. writer.AddAttribute ("showsummary", ShowSummary.ToString());
  146. if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
  147. writer.AddAttribute ("displaymode", DisplayMode.ToString());
  148. writer.AddStyleAttribute ("display", "none");
  149. }
  150. }
  151. bool pre_render_called = false;
  152. #if NET_2_0
  153. protected internal
  154. #else
  155. protected
  156. #endif
  157. override void OnPreRender(EventArgs e) {
  158. base.OnPreRender (e);
  159. pre_render_called = true;
  160. }
  161. #if NET_2_0
  162. protected internal
  163. #else
  164. protected
  165. #endif
  166. override void Render(HtmlTextWriter writer) {
  167. ValidatorCollection validators;
  168. ArrayList errors;
  169. // First, figure out if there's even data to deal with
  170. #if NET_2_0
  171. validators = Page.GetValidators (ValidationGroup);
  172. #else
  173. validators = Page.Validators;
  174. #endif
  175. if (validators.Count == 0) {
  176. return;
  177. }
  178. if (EnableClientScript && pre_render_called) {
  179. Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
  180. String.Format ("document.getElementById ('{0}')", ID));
  181. }
  182. // We have validators
  183. errors = new ArrayList(validators.Count);
  184. for (int i = 0; i < validators.Count; i++) {
  185. if (!validators[i].IsValid) {
  186. errors.Add(validators[i].ErrorMessage);
  187. }
  188. }
  189. if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
  190. base.RenderBeginTag(writer);
  191. if (ShowSummary && errors.Count > 0) {
  192. switch(DisplayMode) {
  193. case ValidationSummaryDisplayMode.BulletList: {
  194. if (HeaderText.Length > 0) {
  195. writer.Write(HeaderText);
  196. }
  197. writer.Write("<ul>");
  198. for (int i = 0; i < errors.Count; i++) {
  199. writer.Write("<li>");
  200. writer.Write(errors[i]);
  201. writer.Write("</li>");
  202. }
  203. writer.Write("</ul>");
  204. break;
  205. }
  206. case ValidationSummaryDisplayMode.List: {
  207. if (HeaderText.Length > 0) {
  208. writer.Write(HeaderText);
  209. #if NET_2_0
  210. writer.Write("<br />");
  211. #else
  212. writer.Write("<br>");
  213. #endif
  214. }
  215. for (int i = 0; i < errors.Count; i++) {
  216. writer.Write(errors[i]);
  217. #if NET_2_0
  218. writer.Write("<br />");
  219. #else
  220. writer.Write("<br>");
  221. #endif
  222. }
  223. break;
  224. }
  225. case ValidationSummaryDisplayMode.SingleParagraph: {
  226. if (HeaderText.Length > 0) {
  227. writer.Write(HeaderText);
  228. writer.Write(" ");
  229. }
  230. for (int i = 0; i < errors.Count; i++) {
  231. writer.Write(errors[i]);
  232. writer.Write(" ");
  233. }
  234. #if NET_2_0
  235. writer.Write("<br />");
  236. #else
  237. writer.Write("<br>");
  238. #endif
  239. break;
  240. }
  241. }
  242. }
  243. if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
  244. base.RenderEndTag(writer);
  245. }
  246. #endregion // Public Instance Methods
  247. }
  248. }