ValidationSummary.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. [MonoTODO ()]
  148. // for 2.0: not XHTML attributes must be registered with RegisterExpandoAttribute
  149. // when it will be implemented, in this case WebUIValidation_2.0.js muist be refactored
  150. protected override void AddAttributesToRender(HtmlTextWriter writer) {
  151. base.AddAttributesToRender (writer);
  152. #if NET_2_0
  153. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
  154. #else
  155. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
  156. #endif
  157. /* force an ID here if we weren't assigned one */
  158. if (ID == null)
  159. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  160. #if NET_2_0
  161. if (ValidationGroup != String.Empty)
  162. writer.AddAttribute ("validationgroup", ValidationGroup);
  163. #endif
  164. if (HeaderText != "")
  165. writer.AddAttribute ("headertext", HeaderText);
  166. if (ShowMessageBox)
  167. writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
  168. if (ShowSummary == false)
  169. writer.AddAttribute ("showsummary", ShowSummary.ToString());
  170. if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
  171. writer.AddAttribute ("displaymode", DisplayMode.ToString());
  172. if (!has_errors)
  173. writer.AddStyleAttribute ("display", "none");
  174. }
  175. }
  176. #if NET_2_0
  177. protected internal
  178. #else
  179. protected
  180. #endif
  181. override void OnPreRender(EventArgs e) {
  182. base.OnPreRender (e);
  183. pre_render_called = true;
  184. }
  185. #if NET_2_0
  186. protected internal
  187. #else
  188. protected
  189. #endif
  190. override void Render(HtmlTextWriter writer) {
  191. #if NET_2_0
  192. if (!Enabled)
  193. return;
  194. #endif
  195. ValidatorCollection validators;
  196. ArrayList errors;
  197. // First, figure out if there's even data to deal with
  198. #if NET_2_0
  199. validators = Page.GetValidators (ValidationGroup);
  200. #else
  201. validators = Page.Validators;
  202. #endif
  203. // We have validators
  204. errors = new ArrayList(validators.Count);
  205. for (int i = 0; i < validators.Count; i++) {
  206. if (!validators[i].IsValid) {
  207. errors.Add(validators[i].ErrorMessage);
  208. }
  209. }
  210. has_errors = errors.Count > 0;
  211. #if NET_2_0
  212. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
  213. #else
  214. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
  215. #endif
  216. Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
  217. String.Format ("document.getElementById ('{0}')", ClientID));
  218. }
  219. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  220. base.RenderBeginTag(writer);
  221. if (ShowSummary && has_errors) {
  222. switch(DisplayMode) {
  223. case ValidationSummaryDisplayMode.BulletList: {
  224. if (HeaderText.Length > 0) {
  225. writer.Write(HeaderText);
  226. }
  227. writer.Write("<ul>");
  228. for (int i = 0; i < errors.Count; i++) {
  229. writer.Write("<li>");
  230. writer.Write(errors[i]);
  231. writer.Write("</li>");
  232. }
  233. writer.Write("</ul>");
  234. break;
  235. }
  236. case ValidationSummaryDisplayMode.List: {
  237. if (HeaderText.Length > 0) {
  238. writer.Write(HeaderText);
  239. #if NET_2_0
  240. writer.Write("<br />");
  241. #else
  242. writer.Write("<br>");
  243. #endif
  244. }
  245. for (int i = 0; i < errors.Count; i++) {
  246. writer.Write(errors[i]);
  247. #if NET_2_0
  248. writer.Write("<br />");
  249. #else
  250. writer.Write("<br>");
  251. #endif
  252. }
  253. break;
  254. }
  255. case ValidationSummaryDisplayMode.SingleParagraph: {
  256. if (HeaderText.Length > 0) {
  257. writer.Write(HeaderText);
  258. writer.Write(" ");
  259. }
  260. for (int i = 0; i < errors.Count; i++) {
  261. writer.Write(errors[i]);
  262. writer.Write(" ");
  263. }
  264. #if NET_2_0
  265. writer.Write("<br />");
  266. #else
  267. writer.Write("<br>");
  268. #endif
  269. break;
  270. }
  271. }
  272. }
  273. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  274. base.RenderEndTag(writer);
  275. }
  276. #endregion // Public Instance Methods
  277. bool pre_render_called;
  278. bool has_errors;
  279. }
  280. }