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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
27 using System.Collections;
28 using System.ComponentModel;
30 using System.Globalization;
31 using System.Security.Permissions;
33 namespace System.Web.UI.WebControls {
36 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
41 public class ValidationSummary : WebControl {
42 #region Public Constructors
43 public ValidationSummary() : base(HtmlTextWriterTag.Div) {
44 this.ForeColor = Color.Red;
46 #endregion // Public Constructors
48 #region Public Instance Properties
52 [DefaultValue(ValidationSummaryDisplayMode.BulletList)]
53 [WebSysDescription ("")]
54 [WebCategory ("Appearance")]
55 public ValidationSummaryDisplayMode DisplayMode {
59 obj = ViewState["DisplayMode"];
61 return (ValidationSummaryDisplayMode)obj;
63 return ValidationSummaryDisplayMode.BulletList;
67 ViewState["DisplayMode"] = value;
75 [WebSysDescription ("")]
76 [WebCategory ("Behavior")]
77 public bool EnableClientScript {
79 return ViewState.GetBool("EnableClientScript", true);
83 ViewState["EnableClientScript"] = value;
87 [DefaultValue(typeof (Color), "Red")]
88 public override System.Drawing.Color ForeColor {
90 return base.ForeColor;
94 base.ForeColor = value;
105 [WebSysDescription ("")]
106 [WebCategory ("Appearance")]
107 public string HeaderText {
109 return ViewState.GetString("HeaderText", string.Empty);
113 ViewState["HeaderText"] = value;
120 [DefaultValue(false)]
121 [WebSysDescription ("")]
122 [WebCategory ("Behavior")]
123 public bool ShowMessageBox {
125 return ViewState.GetBool("ShowMessageBox", false);
129 ViewState["ShowMessageBox"] = value;
137 [WebSysDescription ("")]
138 [WebCategory ("Behavior")]
139 public bool ShowSummary {
141 return ViewState.GetBool("ShowSummary", true);
145 ViewState["ShowSummary"] = value;
152 public string ValidationGroup
155 return ViewState.GetString("ValidationGroup", string.Empty);
158 ViewState["ValidationGroup"] = value;
163 #endregion // Public Instance Properties
165 #region Public Instance Methods
166 protected override void AddAttributesToRender(HtmlTextWriter writer) {
167 base.AddAttributesToRender (writer);
169 if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
170 /* force an ID here if we weren't assigned one */
172 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
174 if (HeaderText != "")
175 writer.AddAttribute ("headertext", HeaderText);
178 writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
180 if (ShowSummary == false)
181 writer.AddAttribute ("showsummary", ShowSummary.ToString());
183 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
184 writer.AddAttribute ("displaymode", DisplayMode.ToString());
187 writer.AddStyleAttribute ("display", "none");
196 override void OnPreRender(EventArgs e) {
197 base.OnPreRender (e);
199 pre_render_called = true;
207 override void Render(HtmlTextWriter writer) {
208 ValidatorCollection validators;
211 // First, figure out if there's even data to deal with
213 validators = Page.GetValidators (ValidationGroup);
215 validators = Page.Validators;
217 if (validators.Count == 0) {
221 // We have validators
222 errors = new ArrayList(validators.Count);
223 for (int i = 0; i < validators.Count; i++) {
224 if (!validators[i].IsValid) {
225 errors.Add(validators[i].ErrorMessage);
229 has_errors = errors.Count > 0;
231 if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
232 Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
233 String.Format ("document.getElementById ('{0}')", ClientID));
236 if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
237 base.RenderBeginTag(writer);
239 if (ShowSummary && has_errors) {
240 switch(DisplayMode) {
241 case ValidationSummaryDisplayMode.BulletList: {
242 if (HeaderText.Length > 0) {
243 writer.Write(HeaderText);
246 writer.Write("<ul>");
247 for (int i = 0; i < errors.Count; i++) {
248 writer.Write("<li>");
249 writer.Write(errors[i]);
250 writer.Write("</li>");
252 writer.Write("</ul>");
256 case ValidationSummaryDisplayMode.List: {
257 if (HeaderText.Length > 0) {
258 writer.Write(HeaderText);
260 writer.Write("<br />");
262 writer.Write("<br>");
266 for (int i = 0; i < errors.Count; i++) {
267 writer.Write(errors[i]);
269 writer.Write("<br />");
271 writer.Write("<br>");
277 case ValidationSummaryDisplayMode.SingleParagraph: {
278 if (HeaderText.Length > 0) {
279 writer.Write(HeaderText);
283 for (int i = 0; i < errors.Count; i++) {
284 writer.Write(errors[i]);
288 writer.Write("<br />");
290 writer.Write("<br>");
298 if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
299 base.RenderEndTag(writer);
301 #endregion // Public Instance Methods
303 bool pre_render_called;