ValidationSummary.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //
  2. // System.Web.UI.WebControls.ValidationSummary.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.ComponentModel;
  33. using System.Drawing;
  34. using System.Web;
  35. using System.Web.UI;
  36. namespace System.Web.UI.WebControls
  37. {
  38. public class ValidationSummary : WebControl
  39. {
  40. private bool uplevelRender;
  41. public ValidationSummary(): base(HtmlTextWriterTag.Div)
  42. {
  43. uplevelRender = false;
  44. ForeColor = Color.Red;
  45. }
  46. [DefaultValue (typeof (ValidationSummaryDisplayMode), "BulletList"), Bindable (true), WebCategory ("Appearance")]
  47. [WebSysDescription ("The type of how validation summaries should be displayed.")]
  48. public ValidationSummaryDisplayMode DisplayMode
  49. {
  50. get
  51. {
  52. object o = ViewState["DisplayMode"];
  53. if(o != null)
  54. return (ValidationSummaryDisplayMode)o;
  55. return ValidationSummaryDisplayMode.BulletList;
  56. }
  57. set
  58. {
  59. if(!Enum.IsDefined(typeof(ValidationSummaryDisplayMode), value))
  60. throw new ArgumentException();
  61. ViewState["DisplayMode"] = value;
  62. }
  63. }
  64. [DefaultValue (true), WebCategory ("Behavior")]
  65. [WebSysDescription ("Determines if the validation summary should be updated directly on the client using script code.")]
  66. public bool EnableClientScript
  67. {
  68. get
  69. {
  70. object o = ViewState["EnableClientScript"];
  71. if(o != null)
  72. return (bool)o;
  73. return true;
  74. }
  75. set
  76. {
  77. ViewState["EnableClientScript"] = value;
  78. }
  79. }
  80. [DefaultValue (null)]
  81. public override Color ForeColor
  82. {
  83. get
  84. {
  85. return base.ForeColor;
  86. }
  87. set
  88. {
  89. base.ForeColor = value;
  90. }
  91. }
  92. [DefaultValue (false), Bindable (true), WebCategory ("Behavior")]
  93. [WebSysDescription ("Determines if the validation summary should display a message box on the client if an uplevel browser is used.")]
  94. public bool ShowMessageBox
  95. {
  96. get
  97. {
  98. object o = ViewState["ShowMessageBox"];
  99. if(o != null)
  100. return (bool)o;
  101. return false;
  102. }
  103. set
  104. {
  105. ViewState["ShowMessageBox"] = value;
  106. }
  107. }
  108. [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
  109. [WebSysDescription ("Determines if the validation summary should display a summary.")]
  110. public bool ShowSummary
  111. {
  112. get
  113. {
  114. object o = ViewState["ShowSummary"];
  115. if(o != null)
  116. return (bool)o;
  117. return true;
  118. }
  119. set
  120. {
  121. ViewState["ShowSummary"] = value;
  122. }
  123. }
  124. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  125. [WebSysDescription ("A text that is diplayed as a header for the validation report.")]
  126. public string HeaderText
  127. {
  128. get
  129. {
  130. object o = ViewState["HeaderText"];
  131. if(o != null)
  132. return (string)o;
  133. return String.Empty;
  134. }
  135. set
  136. {
  137. ViewState["HeaderText"] = value;
  138. }
  139. }
  140. protected override void AddAttributesToRender(HtmlTextWriter writer)
  141. {
  142. base.AddAttributesToRender(writer);
  143. if(uplevelRender)
  144. {
  145. if(ID == null || ID.Length == 0)
  146. writer.AddAttribute("id", ClientID);
  147. if(HeaderText.Length > 0)
  148. writer.AddAttribute("headertext", HeaderText, true);
  149. if(ShowMessageBox)
  150. writer.AddAttribute("showmessagebox", "True");
  151. if(!ShowSummary)
  152. writer.AddAttribute("showsummary", "False");
  153. if(DisplayMode != ValidationSummaryDisplayMode.BulletList)
  154. {
  155. writer.AddAttribute("displaymode", PropertyConverter.EnumToString(typeof(ValidationSummaryDisplayMode), DisplayMode));
  156. }
  157. }
  158. }
  159. protected override void OnPreRender(EventArgs e)
  160. {
  161. }
  162. protected override void Render(HtmlTextWriter writer)
  163. {
  164. if (!base.Enabled) return;
  165. string[] messages;
  166. bool toDisplay;
  167. bool showSummary;
  168. if (base.Site != null && base.Site.DesignMode)
  169. {
  170. showSummary = true;
  171. messages = new string[]{HttpRuntime.FormatResourceString("ValSummary_error_message_1"),
  172. HttpRuntime.FormatResourceString("ValSummary_error_message_2")};
  173. toDisplay = true;
  174. uplevelRender = false;
  175. }
  176. else
  177. {
  178. showSummary = false;
  179. messages = null;
  180. //Messages count
  181. int numOfMsg = 0;
  182. for (int i = 0; i < base.Page.Validators.Count; i++)
  183. {
  184. IValidator currentValidator = base.Page.Validators[i];
  185. if (!currentValidator.IsValid)
  186. {
  187. showSummary = true;
  188. if (currentValidator.ErrorMessage != null &&
  189. currentValidator.ErrorMessage.Length != 0)
  190. numOfMsg++;
  191. }
  192. }
  193. if (numOfMsg != 0)
  194. {
  195. messages = new string[(int)numOfMsg];
  196. int cur_msg = 0;
  197. for (int i = 0; i < base.Page.Validators.Count; i++)
  198. {
  199. IValidator currentValidator = base.Page.Validators[i];
  200. if (!currentValidator.IsValid &&
  201. currentValidator.ErrorMessage != null &&
  202. currentValidator.ErrorMessage.Length != 0)
  203. messages[cur_msg++] = String.Copy(currentValidator.ErrorMessage);
  204. }
  205. }
  206. toDisplay = ShowSummary ? showSummary : false;
  207. if (!toDisplay && uplevelRender)
  208. base.Style["display"] = "none";
  209. }
  210. if (base.Page != null)
  211. {
  212. base.Page.VerifyRenderingInServerForm(this);
  213. }
  214. bool tagRequired = !uplevelRender ? toDisplay : true;
  215. if (tagRequired)
  216. {
  217. base.RenderBeginTag(writer);
  218. }
  219. if (toDisplay)
  220. {
  221. string str1, str2, str3, str4, str5;
  222. switch (DisplayMode)
  223. {
  224. case ValidationSummaryDisplayMode.List:
  225. str1 = "<br>";
  226. str2 = "";
  227. str3 = "";
  228. str4 = "<br>";
  229. str5 = "";
  230. break;
  231. case ValidationSummaryDisplayMode.SingleParagraph:
  232. str1 = " ";
  233. str2 = "";
  234. str3 = "";
  235. str4 = " ";
  236. str5 = "<br>";
  237. break;
  238. default:
  239. str1 = "";
  240. str2 = "<ul>";
  241. str3 = "<li>";
  242. str4 = "</li>";
  243. str5 = "</ul>";
  244. break;
  245. }
  246. if (HeaderText.Length > 0)
  247. {
  248. writer.Write(HeaderText);
  249. writer.Write(str1);
  250. }
  251. writer.Write(str2);
  252. if (messages != null)
  253. {
  254. for (int i = 0; i < (int)messages.Length; i++)
  255. {
  256. writer.Write(str3);
  257. writer.Write(messages[i]);
  258. writer.Write(str4);
  259. }
  260. }
  261. writer.Write(str5);
  262. }
  263. if (tagRequired)
  264. {
  265. base.RenderEndTag(writer);
  266. }
  267. }
  268. }
  269. }