ValidationSummary.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.Collections;
  27. using System.ComponentModel;
  28. using System.Drawing;
  29. using System.Globalization;
  30. using System.Security.Permissions;
  31. namespace System.Web.UI.WebControls {
  32. // CAS
  33. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  34. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  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 virtual 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 && Page.AreValidatorsUplevel ()) {
  150. /* force an ID here if we weren't assigned one */
  151. if (ID == null)
  152. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  153. if (HeaderText != "")
  154. writer.AddAttribute ("headertext", HeaderText);
  155. if (ShowMessageBox)
  156. writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
  157. if (ShowSummary == false)
  158. writer.AddAttribute ("showsummary", ShowSummary.ToString());
  159. if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
  160. writer.AddAttribute ("displaymode", DisplayMode.ToString());
  161. if (!has_errors)
  162. writer.AddStyleAttribute ("display", "none");
  163. }
  164. }
  165. #if NET_2_0
  166. protected internal
  167. #else
  168. protected
  169. #endif
  170. override void OnPreRender(EventArgs e) {
  171. base.OnPreRender (e);
  172. pre_render_called = true;
  173. }
  174. #if NET_2_0
  175. protected internal
  176. #else
  177. protected
  178. #endif
  179. override void Render(HtmlTextWriter writer) {
  180. ValidatorCollection validators;
  181. ArrayList errors;
  182. // First, figure out if there's even data to deal with
  183. #if NET_2_0
  184. validators = Page.GetValidators (ValidationGroup);
  185. #else
  186. validators = Page.Validators;
  187. #endif
  188. if (validators.Count == 0) {
  189. return;
  190. }
  191. // We have validators
  192. errors = new ArrayList(validators.Count);
  193. for (int i = 0; i < validators.Count; i++) {
  194. if (!validators[i].IsValid) {
  195. errors.Add(validators[i].ErrorMessage);
  196. }
  197. }
  198. has_errors = errors.Count > 0;
  199. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
  200. Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
  201. String.Format ("document.getElementById ('{0}')", ClientID));
  202. }
  203. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  204. base.RenderBeginTag(writer);
  205. if (ShowSummary && has_errors) {
  206. switch(DisplayMode) {
  207. case ValidationSummaryDisplayMode.BulletList: {
  208. if (HeaderText.Length > 0) {
  209. writer.Write(HeaderText);
  210. }
  211. writer.Write("<ul>");
  212. for (int i = 0; i < errors.Count; i++) {
  213. writer.Write("<li>");
  214. writer.Write(errors[i]);
  215. writer.Write("</li>");
  216. }
  217. writer.Write("</ul>");
  218. break;
  219. }
  220. case ValidationSummaryDisplayMode.List: {
  221. if (HeaderText.Length > 0) {
  222. writer.Write(HeaderText);
  223. #if NET_2_0
  224. writer.Write("<br />");
  225. #else
  226. writer.Write("<br>");
  227. #endif
  228. }
  229. for (int i = 0; i < errors.Count; i++) {
  230. writer.Write(errors[i]);
  231. #if NET_2_0
  232. writer.Write("<br />");
  233. #else
  234. writer.Write("<br>");
  235. #endif
  236. }
  237. break;
  238. }
  239. case ValidationSummaryDisplayMode.SingleParagraph: {
  240. if (HeaderText.Length > 0) {
  241. writer.Write(HeaderText);
  242. writer.Write(" ");
  243. }
  244. for (int i = 0; i < errors.Count; i++) {
  245. writer.Write(errors[i]);
  246. writer.Write(" ");
  247. }
  248. #if NET_2_0
  249. writer.Write("<br />");
  250. #else
  251. writer.Write("<br>");
  252. #endif
  253. break;
  254. }
  255. }
  256. }
  257. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  258. base.RenderEndTag(writer);
  259. }
  260. #endregion // Public Instance Methods
  261. bool pre_render_called;
  262. bool has_errors;
  263. }
  264. }