ValidationSummary.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. Page.ClientScript.RegisterExpandoAttribute (ClientID, "validationGroup", ValidationGroup);
  163. if (HeaderText.Length > 0)
  164. Page.ClientScript.RegisterExpandoAttribute (ClientID, "headertext", HeaderText);
  165. if (ShowMessageBox)
  166. Page.ClientScript.RegisterExpandoAttribute (ClientID, "showmessagebox", "True");
  167. if (!ShowSummary)
  168. Page.ClientScript.RegisterExpandoAttribute (ClientID, "showsummary", "False");
  169. if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
  170. Page.ClientScript.RegisterExpandoAttribute (ClientID, "displaymode", DisplayMode.ToString ());
  171. #else
  172. if (HeaderText != "")
  173. writer.AddAttribute ("headertext", HeaderText);
  174. if (ShowMessageBox)
  175. writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
  176. if (ShowSummary == false)
  177. writer.AddAttribute ("showsummary", ShowSummary.ToString());
  178. if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
  179. writer.AddAttribute ("displaymode", DisplayMode.ToString());
  180. #endif
  181. if (!has_errors)
  182. writer.AddStyleAttribute ("display", "none");
  183. }
  184. }
  185. #if NET_2_0
  186. protected internal
  187. #else
  188. protected
  189. #endif
  190. override void OnPreRender(EventArgs e) {
  191. base.OnPreRender (e);
  192. pre_render_called = true;
  193. }
  194. #if NET_2_0
  195. protected internal
  196. #else
  197. protected
  198. #endif
  199. override void Render(HtmlTextWriter writer) {
  200. #if NET_2_0
  201. if (!Enabled)
  202. return;
  203. #endif
  204. ValidatorCollection validators;
  205. ArrayList errors;
  206. // First, figure out if there's even data to deal with
  207. #if NET_2_0
  208. validators = Page.GetValidators (ValidationGroup);
  209. #else
  210. validators = Page.Validators;
  211. #endif
  212. // We have validators
  213. errors = new ArrayList(validators.Count);
  214. for (int i = 0; i < validators.Count; i++) {
  215. if (!validators[i].IsValid) {
  216. errors.Add(validators[i].ErrorMessage);
  217. }
  218. }
  219. has_errors = errors.Count > 0;
  220. #if NET_2_0
  221. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
  222. #else
  223. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
  224. #endif
  225. Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
  226. String.Concat ("document.getElementById ('", ClientID, "')"));
  227. }
  228. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  229. base.RenderBeginTag(writer);
  230. if (ShowSummary && has_errors) {
  231. switch(DisplayMode) {
  232. case ValidationSummaryDisplayMode.BulletList: {
  233. if (HeaderText.Length > 0) {
  234. writer.Write(HeaderText);
  235. }
  236. writer.Write("<ul>");
  237. for (int i = 0; i < errors.Count; i++) {
  238. writer.Write("<li>");
  239. writer.Write(errors[i]);
  240. writer.Write("</li>");
  241. }
  242. writer.Write("</ul>");
  243. break;
  244. }
  245. case ValidationSummaryDisplayMode.List: {
  246. if (HeaderText.Length > 0) {
  247. writer.Write(HeaderText);
  248. #if NET_2_0
  249. writer.Write("<br />");
  250. #else
  251. writer.Write("<br>");
  252. #endif
  253. }
  254. for (int i = 0; i < errors.Count; i++) {
  255. writer.Write(errors[i]);
  256. #if NET_2_0
  257. writer.Write("<br />");
  258. #else
  259. writer.Write("<br>");
  260. #endif
  261. }
  262. break;
  263. }
  264. case ValidationSummaryDisplayMode.SingleParagraph: {
  265. if (HeaderText.Length > 0) {
  266. writer.Write(HeaderText);
  267. writer.Write(" ");
  268. }
  269. for (int i = 0; i < errors.Count; i++) {
  270. writer.Write(errors[i]);
  271. writer.Write(" ");
  272. }
  273. #if NET_2_0
  274. writer.Write("<br />");
  275. #else
  276. writer.Write("<br>");
  277. #endif
  278. break;
  279. }
  280. }
  281. }
  282. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  283. base.RenderEndTag(writer);
  284. }
  285. #endregion // Public Instance Methods
  286. bool pre_render_called;
  287. bool has_errors;
  288. }
  289. }