ValidationSummary.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. [WebSysDescription ("")]
  50. [WebCategory ("Appearance")]
  51. public ValidationSummaryDisplayMode DisplayMode {
  52. get {
  53. object obj;
  54. obj = ViewState["DisplayMode"];
  55. if (obj != null) {
  56. return (ValidationSummaryDisplayMode)obj;
  57. }
  58. return ValidationSummaryDisplayMode.BulletList;
  59. }
  60. set {
  61. ViewState["DisplayMode"] = value;
  62. }
  63. }
  64. [DefaultValue(true)]
  65. #if NET_2_0
  66. [Themeable (false)]
  67. #endif
  68. [WebSysDescription ("")]
  69. [WebCategory ("Behavior")]
  70. public bool EnableClientScript {
  71. get {
  72. return ViewState.GetBool("EnableClientScript", true);
  73. }
  74. set {
  75. ViewState["EnableClientScript"] = value;
  76. }
  77. }
  78. [DefaultValue(typeof (Color), "Red")]
  79. public override System.Drawing.Color ForeColor {
  80. get {
  81. return base.ForeColor;
  82. }
  83. set {
  84. base.ForeColor = value;
  85. }
  86. }
  87. #if ONLY_1_1
  88. [Bindable(true)]
  89. #endif
  90. [DefaultValue("")]
  91. #if NET_2_0
  92. [Localizable (true)]
  93. #endif
  94. [WebSysDescription ("")]
  95. [WebCategory ("Appearance")]
  96. public string HeaderText {
  97. get {
  98. return ViewState.GetString("HeaderText", string.Empty);
  99. }
  100. set {
  101. ViewState["HeaderText"] = value;
  102. }
  103. }
  104. #if ONLY_1_1
  105. [Bindable(true)]
  106. #endif
  107. [DefaultValue(false)]
  108. [WebSysDescription ("")]
  109. [WebCategory ("Behavior")]
  110. public bool ShowMessageBox {
  111. get {
  112. return ViewState.GetBool("ShowMessageBox", false);
  113. }
  114. set {
  115. ViewState["ShowMessageBox"] = value;
  116. }
  117. }
  118. #if ONLY_1_1
  119. [Bindable(true)]
  120. #endif
  121. [DefaultValue(true)]
  122. [WebSysDescription ("")]
  123. [WebCategory ("Behavior")]
  124. public bool ShowSummary {
  125. get {
  126. return ViewState.GetBool("ShowSummary", true);
  127. }
  128. set {
  129. ViewState["ShowSummary"] = value;
  130. }
  131. }
  132. #if NET_2_0
  133. [DefaultValue ("")]
  134. [Themeable (false)]
  135. public string ValidationGroup
  136. {
  137. get {
  138. return ViewState.GetString("ValidationGroup", string.Empty);
  139. }
  140. set {
  141. ViewState["ValidationGroup"] = value;
  142. }
  143. }
  144. #endif
  145. #endregion // Public Instance Properties
  146. #region Public Instance Methods
  147. protected override void AddAttributesToRender(HtmlTextWriter writer) {
  148. base.AddAttributesToRender (writer);
  149. if (EnableClientScript && pre_render_called) {
  150. if (HeaderText != "")
  151. writer.AddAttribute ("headertext", HeaderText);
  152. if (ShowMessageBox)
  153. writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
  154. if (ShowSummary == false)
  155. writer.AddAttribute ("showsummary", ShowSummary.ToString());
  156. if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
  157. writer.AddAttribute ("displaymode", DisplayMode.ToString());
  158. writer.AddStyleAttribute ("display", "none");
  159. }
  160. }
  161. bool pre_render_called = false;
  162. #if NET_2_0
  163. protected internal
  164. #else
  165. protected
  166. #endif
  167. override void OnPreRender(EventArgs e) {
  168. base.OnPreRender (e);
  169. pre_render_called = true;
  170. }
  171. #if NET_2_0
  172. protected internal
  173. #else
  174. protected
  175. #endif
  176. override void Render(HtmlTextWriter writer) {
  177. ValidatorCollection validators;
  178. ArrayList errors;
  179. // First, figure out if there's even data to deal with
  180. #if NET_2_0
  181. validators = Page.GetValidators (ValidationGroup);
  182. #else
  183. validators = Page.Validators;
  184. #endif
  185. if (validators.Count == 0) {
  186. return;
  187. }
  188. if (EnableClientScript && pre_render_called) {
  189. Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
  190. String.Format ("document.getElementById ('{0}')", ID));
  191. }
  192. // We have validators
  193. errors = new ArrayList(validators.Count);
  194. for (int i = 0; i < validators.Count; i++) {
  195. if (!validators[i].IsValid) {
  196. errors.Add(validators[i].ErrorMessage);
  197. }
  198. }
  199. if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
  200. base.RenderBeginTag(writer);
  201. if (ShowSummary && errors.Count > 0) {
  202. switch(DisplayMode) {
  203. case ValidationSummaryDisplayMode.BulletList: {
  204. if (HeaderText.Length > 0) {
  205. writer.Write(HeaderText);
  206. }
  207. writer.Write("<ul>");
  208. for (int i = 0; i < errors.Count; i++) {
  209. writer.Write("<li>");
  210. writer.Write(errors[i]);
  211. writer.Write("</li>");
  212. }
  213. writer.Write("</ul>");
  214. break;
  215. }
  216. case ValidationSummaryDisplayMode.List: {
  217. if (HeaderText.Length > 0) {
  218. writer.Write(HeaderText);
  219. #if NET_2_0
  220. writer.Write("<br />");
  221. #else
  222. writer.Write("<br>");
  223. #endif
  224. }
  225. for (int i = 0; i < errors.Count; i++) {
  226. writer.Write(errors[i]);
  227. #if NET_2_0
  228. writer.Write("<br />");
  229. #else
  230. writer.Write("<br>");
  231. #endif
  232. }
  233. break;
  234. }
  235. case ValidationSummaryDisplayMode.SingleParagraph: {
  236. if (HeaderText.Length > 0) {
  237. writer.Write(HeaderText);
  238. writer.Write(" ");
  239. }
  240. for (int i = 0; i < errors.Count; i++) {
  241. writer.Write(errors[i]);
  242. writer.Write(" ");
  243. }
  244. #if NET_2_0
  245. writer.Write("<br />");
  246. #else
  247. writer.Write("<br>");
  248. #endif
  249. break;
  250. }
  251. }
  252. }
  253. if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
  254. base.RenderEndTag(writer);
  255. }
  256. #endregion // Public Instance Methods
  257. }
  258. }