ValidationSummary.cs 8.5 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.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. [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  36. public class ValidationSummary : WebControl {
  37. #region Public Constructors
  38. public ValidationSummary() : base(HtmlTextWriterTag.Div) {
  39. this.ForeColor = Color.Red;
  40. }
  41. #endregion // Public Constructors
  42. #region Public Instance Properties
  43. [DefaultValue(ValidationSummaryDisplayMode.BulletList)]
  44. [WebSysDescription ("")]
  45. [WebCategory ("Appearance")]
  46. public ValidationSummaryDisplayMode DisplayMode {
  47. get {
  48. object obj;
  49. obj = ViewState["DisplayMode"];
  50. if (obj != null) {
  51. return (ValidationSummaryDisplayMode)obj;
  52. }
  53. return ValidationSummaryDisplayMode.BulletList;
  54. }
  55. set {
  56. ViewState["DisplayMode"] = value;
  57. }
  58. }
  59. [DefaultValue(true)]
  60. [Themeable (false)]
  61. [WebSysDescription ("")]
  62. [WebCategory ("Behavior")]
  63. public bool EnableClientScript {
  64. get {
  65. return ViewState.GetBool("EnableClientScript", true);
  66. }
  67. set {
  68. ViewState["EnableClientScript"] = value;
  69. }
  70. }
  71. [DefaultValue(typeof (Color), "Red")]
  72. public override System.Drawing.Color ForeColor {
  73. get {
  74. return base.ForeColor;
  75. }
  76. set {
  77. base.ForeColor = value;
  78. }
  79. }
  80. [DefaultValue("")]
  81. [Localizable (true)]
  82. [WebSysDescription ("")]
  83. [WebCategory ("Appearance")]
  84. public string HeaderText {
  85. get {
  86. return ViewState.GetString("HeaderText", string.Empty);
  87. }
  88. set {
  89. ViewState["HeaderText"] = value;
  90. }
  91. }
  92. [DefaultValue(false)]
  93. [WebSysDescription ("")]
  94. [WebCategory ("Behavior")]
  95. public bool ShowMessageBox {
  96. get {
  97. return ViewState.GetBool("ShowMessageBox", false);
  98. }
  99. set {
  100. ViewState["ShowMessageBox"] = value;
  101. }
  102. }
  103. [DefaultValue(true)]
  104. [WebSysDescription ("")]
  105. [WebCategory ("Behavior")]
  106. public bool ShowSummary {
  107. get {
  108. return ViewState.GetBool("ShowSummary", true);
  109. }
  110. set {
  111. ViewState["ShowSummary"] = value;
  112. }
  113. }
  114. [DefaultValue ("")]
  115. [Themeable (false)]
  116. public virtual string ValidationGroup
  117. {
  118. get {
  119. return ViewState.GetString("ValidationGroup", string.Empty);
  120. }
  121. set {
  122. ViewState["ValidationGroup"] = value;
  123. }
  124. }
  125. public override bool SupportsDisabledAttribute {
  126. get { return RenderingCompatibilityLessThan40; }
  127. }
  128. #endregion // Public Instance Properties
  129. #region Public Instance Methods
  130. [MonoTODO ()]
  131. // for 2.0: not XHTML attributes must be registered with RegisterExpandoAttribute
  132. // when it will be implemented, in this case WebUIValidation_2.0.js muist be refactored
  133. protected override void AddAttributesToRender(HtmlTextWriter writer) {
  134. base.AddAttributesToRender (writer);
  135. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
  136. /* force an ID here if we weren't assigned one */
  137. if (ID == null)
  138. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  139. if (ValidationGroup != String.Empty)
  140. RegisterExpandoAttribute (ClientID, "validationGroup", ValidationGroup);
  141. if (HeaderText.Length > 0)
  142. RegisterExpandoAttribute (ClientID, "headertext", HeaderText);
  143. if (ShowMessageBox)
  144. RegisterExpandoAttribute (ClientID, "showmessagebox", "True");
  145. if (!ShowSummary)
  146. RegisterExpandoAttribute (ClientID, "showsummary", "False");
  147. if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
  148. RegisterExpandoAttribute (ClientID, "displaymode", DisplayMode.ToString ());
  149. if (!has_errors)
  150. writer.AddStyleAttribute ("display", "none");
  151. }
  152. }
  153. internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue) {
  154. RegisterExpandoAttribute (controlId, attributeName, attributeValue, false);
  155. }
  156. internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue, bool encode) {
  157. if (Page.ScriptManager != null)
  158. Page.ScriptManager.RegisterExpandoAttributeExternal (this, controlId, attributeName, attributeValue, encode);
  159. else
  160. Page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
  161. }
  162. protected internal override void OnInit (EventArgs e)
  163. {
  164. base.OnInit (e);
  165. if (!RenderingCompatibilityLessThan40)
  166. return;
  167. if (ForeColor == Color.Empty)
  168. ForeColor = Color.Red;
  169. }
  170. protected internal
  171. override void OnPreRender(EventArgs e) {
  172. base.OnPreRender (e);
  173. pre_render_called = true;
  174. }
  175. protected internal
  176. override void Render(HtmlTextWriter writer) {
  177. if (!IsEnabled)
  178. return;
  179. ValidatorCollection validators;
  180. ArrayList errors;
  181. // First, figure out if there's even data to deal with
  182. validators = Page.GetValidators (ValidationGroup);
  183. // We have validators
  184. errors = new ArrayList(validators.Count);
  185. for (int i = 0; i < validators.Count; i++) {
  186. if (!validators[i].IsValid) {
  187. errors.Add(validators[i].ErrorMessage);
  188. }
  189. }
  190. has_errors = errors.Count > 0;
  191. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
  192. if (Page.ScriptManager != null) {
  193. Page.ScriptManager.RegisterArrayDeclarationExternal (this, "Page_ValidationSummaries", String.Concat ("document.getElementById ('", ClientID, "')"));
  194. Page.ScriptManager.RegisterStartupScriptExternal (this, typeof (BaseValidator), ClientID + "DisposeScript",
  195. @"
  196. document.getElementById('" + ClientID + @"').dispose = function() {
  197. Array.remove(Page_ValidationSummaries, document.getElementById('" + ClientID + @"'));
  198. }
  199. ", true);
  200. }
  201. else
  202. Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
  203. String.Concat ("document.getElementById ('", ClientID, "')"));
  204. }
  205. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  206. base.RenderBeginTag(writer);
  207. if (ShowSummary && has_errors) {
  208. switch(DisplayMode) {
  209. case ValidationSummaryDisplayMode.BulletList: {
  210. if (HeaderText.Length > 0) {
  211. writer.Write(HeaderText);
  212. }
  213. writer.Write("<ul>");
  214. for (int i = 0; i < errors.Count; i++) {
  215. writer.Write("<li>");
  216. writer.Write(errors[i]);
  217. writer.Write("</li>");
  218. }
  219. writer.Write("</ul>");
  220. break;
  221. }
  222. case ValidationSummaryDisplayMode.List: {
  223. if (HeaderText.Length > 0) {
  224. writer.Write(HeaderText);
  225. writer.Write("<br />");
  226. }
  227. for (int i = 0; i < errors.Count; i++) {
  228. writer.Write(errors[i]);
  229. writer.Write("<br />");
  230. }
  231. break;
  232. }
  233. case ValidationSummaryDisplayMode.SingleParagraph: {
  234. if (HeaderText.Length > 0) {
  235. writer.Write(HeaderText);
  236. writer.Write(" ");
  237. }
  238. for (int i = 0; i < errors.Count; i++) {
  239. writer.Write(errors[i]);
  240. writer.Write(" ");
  241. }
  242. writer.Write("<br />");
  243. break;
  244. }
  245. }
  246. }
  247. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  248. base.RenderEndTag(writer);
  249. }
  250. #endregion // Public Instance Methods
  251. bool pre_render_called;
  252. bool has_errors;
  253. }
  254. }