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)
28 using System.Collections;
29 using System.ComponentModel;
31 using System.Globalization;
34 using System.Web.UI.WebControls;
36 namespace System.Web.UI.WebControls {
38 [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40 public class ValidationSummary : WebControl {
41 #region Public Constructors
42 public ValidationSummary() : base(HtmlTextWriterTag.Div) {
43 this.ForeColor = Color.Red;
45 #endregion // Public Constructors
47 #region Public Instance Properties
51 [DefaultValue(ValidationSummaryDisplayMode.BulletList)]
52 [WebSysDescription ("")]
53 [WebCategory ("Appearance")]
54 public ValidationSummaryDisplayMode DisplayMode {
58 obj = ViewState["DisplayMode"];
60 return (ValidationSummaryDisplayMode)obj;
62 return ValidationSummaryDisplayMode.BulletList;
66 ViewState["DisplayMode"] = value;
74 [WebSysDescription ("")]
75 [WebCategory ("Behavior")]
76 public bool EnableClientScript {
78 return ViewState.GetBool("EnableClientScript", true);
82 ViewState["EnableClientScript"] = value;
86 [DefaultValue(typeof (Color), "Red")]
87 public override System.Drawing.Color ForeColor {
89 return base.ForeColor;
93 base.ForeColor = value;
104 [WebSysDescription ("")]
105 [WebCategory ("Appearance")]
106 public string HeaderText {
108 return ViewState.GetString("HeaderText", string.Empty);
112 ViewState["HeaderText"] = value;
119 [DefaultValue(false)]
120 [WebSysDescription ("")]
121 [WebCategory ("Behavior")]
122 public bool ShowMessageBox {
124 return ViewState.GetBool("ShowMessageBox", false);
128 ViewState["ShowMessageBox"] = value;
136 [WebSysDescription ("")]
137 [WebCategory ("Behavior")]
138 public bool ShowSummary {
140 return ViewState.GetBool("ShowSummary", true);
144 ViewState["ShowSummary"] = value;
151 public string ValidationGroup
154 return ViewState.GetString("ValidationGroup", string.Empty);
157 ViewState["ValidationGroup"] = value;
162 #endregion // Public Instance Properties
164 #region Public Instance Methods
165 protected override void AddAttributesToRender(HtmlTextWriter writer) {
166 base.AddAttributesToRender (writer);
168 if (EnableClientScript && pre_render_called) {
169 if (HeaderText != "")
170 writer.AddAttribute ("headertext", HeaderText);
173 writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
175 if (ShowSummary == false)
176 writer.AddAttribute ("showsummary", ShowSummary.ToString());
178 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
179 writer.AddAttribute ("displaymode", DisplayMode.ToString());
181 writer.AddStyleAttribute ("display", "none");
185 bool pre_render_called = false;
192 override void OnPreRender(EventArgs e) {
193 base.OnPreRender (e);
195 pre_render_called = true;
203 override void Render(HtmlTextWriter writer) {
204 ValidatorCollection validators;
207 // First, figure out if there's even data to deal with
209 validators = Page.GetValidators (ValidationGroup);
211 validators = Page.Validators;
213 if (validators.Count == 0) {
217 if (EnableClientScript && pre_render_called) {
218 Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
219 String.Format ("document.getElementById ('{0}')", ID));
222 // We have validators
223 errors = new ArrayList(validators.Count);
224 for (int i = 0; i < validators.Count; i++) {
225 if (!validators[i].IsValid) {
226 errors.Add(validators[i].ErrorMessage);
230 if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
231 base.RenderBeginTag(writer);
233 if (ShowSummary && errors.Count > 0) {
234 switch(DisplayMode) {
235 case ValidationSummaryDisplayMode.BulletList: {
236 if (HeaderText.Length > 0) {
237 writer.Write(HeaderText);
240 writer.Write("<ul>");
241 for (int i = 0; i < errors.Count; i++) {
242 writer.Write("<li>");
243 writer.Write(errors[i]);
244 writer.Write("</li>");
246 writer.Write("</ul>");
250 case ValidationSummaryDisplayMode.List: {
251 if (HeaderText.Length > 0) {
252 writer.Write(HeaderText);
254 writer.Write("<br />");
256 writer.Write("<br>");
260 for (int i = 0; i < errors.Count; i++) {
261 writer.Write(errors[i]);
263 writer.Write("<br />");
265 writer.Write("<br>");
271 case ValidationSummaryDisplayMode.SingleParagraph: {
272 if (HeaderText.Length > 0) {
273 writer.Write(HeaderText);
277 for (int i = 0; i < errors.Count; i++) {
278 writer.Write(errors[i]);
282 writer.Write("<br />");
284 writer.Write("<br>");
292 if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
293 base.RenderEndTag(writer);
295 #endregion // Public Instance Methods