ValidationSummary.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #if NET_4_0
  146. public override bool SupportsDisabledAttribute {
  147. get { return RenderingCompatibilityLessThan40; }
  148. }
  149. #endif
  150. #endregion // Public Instance Properties
  151. #region Public Instance Methods
  152. [MonoTODO ()]
  153. // for 2.0: not XHTML attributes must be registered with RegisterExpandoAttribute
  154. // when it will be implemented, in this case WebUIValidation_2.0.js muist be refactored
  155. protected override void AddAttributesToRender(HtmlTextWriter writer) {
  156. base.AddAttributesToRender (writer);
  157. #if NET_2_0
  158. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
  159. #else
  160. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
  161. #endif
  162. /* force an ID here if we weren't assigned one */
  163. if (ID == null)
  164. writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
  165. #if NET_2_0
  166. if (ValidationGroup != String.Empty)
  167. RegisterExpandoAttribute (ClientID, "validationGroup", ValidationGroup);
  168. if (HeaderText.Length > 0)
  169. RegisterExpandoAttribute (ClientID, "headertext", HeaderText);
  170. if (ShowMessageBox)
  171. RegisterExpandoAttribute (ClientID, "showmessagebox", "True");
  172. if (!ShowSummary)
  173. RegisterExpandoAttribute (ClientID, "showsummary", "False");
  174. if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
  175. RegisterExpandoAttribute (ClientID, "displaymode", DisplayMode.ToString ());
  176. #else
  177. if (HeaderText != "")
  178. writer.AddAttribute ("headertext", HeaderText);
  179. if (ShowMessageBox)
  180. writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
  181. if (ShowSummary == false)
  182. writer.AddAttribute ("showsummary", ShowSummary.ToString());
  183. if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
  184. writer.AddAttribute ("displaymode", DisplayMode.ToString());
  185. #endif
  186. if (!has_errors)
  187. writer.AddStyleAttribute ("display", "none");
  188. }
  189. }
  190. #if NET_2_0
  191. internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue) {
  192. RegisterExpandoAttribute (controlId, attributeName, attributeValue, false);
  193. }
  194. internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue, bool encode) {
  195. if (Page.ScriptManager != null)
  196. Page.ScriptManager.RegisterExpandoAttributeExternal (this, controlId, attributeName, attributeValue, encode);
  197. else
  198. Page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
  199. }
  200. #endif
  201. #if NET_4_0
  202. protected internal override void OnInit (EventArgs e)
  203. {
  204. base.OnInit (e);
  205. if (!RenderingCompatibilityLessThan40)
  206. return;
  207. if (ForeColor == Color.Empty)
  208. ForeColor = Color.Red;
  209. }
  210. #endif
  211. #if NET_2_0
  212. protected internal
  213. #else
  214. protected
  215. #endif
  216. override void OnPreRender(EventArgs e) {
  217. base.OnPreRender (e);
  218. pre_render_called = true;
  219. }
  220. #if NET_2_0
  221. protected internal
  222. #else
  223. protected
  224. #endif
  225. override void Render(HtmlTextWriter writer) {
  226. #if NET_2_0
  227. if (!IsEnabled)
  228. return;
  229. #endif
  230. ValidatorCollection validators;
  231. ArrayList errors;
  232. // First, figure out if there's even data to deal with
  233. #if NET_2_0
  234. validators = Page.GetValidators (ValidationGroup);
  235. #else
  236. validators = Page.Validators;
  237. #endif
  238. // We have validators
  239. errors = new ArrayList(validators.Count);
  240. for (int i = 0; i < validators.Count; i++) {
  241. if (!validators[i].IsValid) {
  242. errors.Add(validators[i].ErrorMessage);
  243. }
  244. }
  245. has_errors = errors.Count > 0;
  246. #if NET_2_0
  247. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
  248. #else
  249. if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
  250. #endif
  251. #if NET_2_0
  252. if (Page.ScriptManager != null) {
  253. Page.ScriptManager.RegisterArrayDeclarationExternal (this, "Page_ValidationSummaries", String.Concat ("document.getElementById ('", ClientID, "')"));
  254. Page.ScriptManager.RegisterStartupScriptExternal (this, typeof (BaseValidator), ClientID + "DisposeScript",
  255. @"
  256. document.getElementById('" + ClientID + @"').dispose = function() {
  257. Array.remove(Page_ValidationSummaries, document.getElementById('" + ClientID + @"'));
  258. }
  259. ", true);
  260. }
  261. else
  262. #endif
  263. Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
  264. String.Concat ("document.getElementById ('", ClientID, "')"));
  265. }
  266. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  267. base.RenderBeginTag(writer);
  268. if (ShowSummary && has_errors) {
  269. switch(DisplayMode) {
  270. case ValidationSummaryDisplayMode.BulletList: {
  271. if (HeaderText.Length > 0) {
  272. writer.Write(HeaderText);
  273. }
  274. writer.Write("<ul>");
  275. for (int i = 0; i < errors.Count; i++) {
  276. writer.Write("<li>");
  277. writer.Write(errors[i]);
  278. writer.Write("</li>");
  279. }
  280. writer.Write("</ul>");
  281. break;
  282. }
  283. case ValidationSummaryDisplayMode.List: {
  284. if (HeaderText.Length > 0) {
  285. writer.Write(HeaderText);
  286. #if NET_2_0
  287. writer.Write("<br />");
  288. #else
  289. writer.Write("<br>");
  290. #endif
  291. }
  292. for (int i = 0; i < errors.Count; i++) {
  293. writer.Write(errors[i]);
  294. #if NET_2_0
  295. writer.Write("<br />");
  296. #else
  297. writer.Write("<br>");
  298. #endif
  299. }
  300. break;
  301. }
  302. case ValidationSummaryDisplayMode.SingleParagraph: {
  303. if (HeaderText.Length > 0) {
  304. writer.Write(HeaderText);
  305. writer.Write(" ");
  306. }
  307. for (int i = 0; i < errors.Count; i++) {
  308. writer.Write(errors[i]);
  309. writer.Write(" ");
  310. }
  311. #if NET_2_0
  312. writer.Write("<br />");
  313. #else
  314. writer.Write("<br>");
  315. #endif
  316. break;
  317. }
  318. }
  319. }
  320. if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
  321. base.RenderEndTag(writer);
  322. }
  323. #endregion // Public Instance Methods
  324. bool pre_render_called;
  325. bool has_errors;
  326. }
  327. }