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 public ValidationSummaryDisplayMode DisplayMode {
56 obj = ViewState["DisplayMode"];
58 return (ValidationSummaryDisplayMode)obj;
60 return ValidationSummaryDisplayMode.BulletList;
64 ViewState["DisplayMode"] = value;
72 public bool EnableClientScript {
74 return ViewState.GetBool("EnableClientScript", true);
78 ViewState["EnableClientScript"] = value;
82 [DefaultValue("Color [Red]")]
83 public override System.Drawing.Color ForeColor {
85 return base.ForeColor;
89 base.ForeColor = value;
100 public string HeaderText {
102 return ViewState.GetString("HeaderText", string.Empty);
106 ViewState["HeaderText"] = value;
113 [DefaultValue(false)]
114 public bool ShowMessageBox {
116 return ViewState.GetBool("ShowMessageBox", false);
120 ViewState["ShowMessageBox"] = value;
128 public bool ShowSummary {
130 return ViewState.GetBool("ShowSummary", true);
134 ViewState["ShowSummary"] = value;
141 public string ValidationGroup
144 return ViewState.GetString("ValidationGroup", string.Empty);
147 ViewState["ValidationGroup"] = value;
152 #endregion // Public Instance Properties
154 #region Public Instance Methods
155 protected override void AddAttributesToRender(HtmlTextWriter writer) {
156 base.AddAttributesToRender (writer);
158 if (EnableClientScript && pre_render_called) {
159 if (HeaderText != "")
160 writer.AddAttribute ("headertext", HeaderText);
163 writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
165 if (ShowSummary == false)
166 writer.AddAttribute ("showsummary", ShowSummary.ToString());
168 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
169 writer.AddAttribute ("displaymode", DisplayMode.ToString());
171 writer.AddStyleAttribute ("display", "none");
175 bool pre_render_called = false;
182 override void OnPreRender(EventArgs e) {
183 base.OnPreRender (e);
185 pre_render_called = true;
193 override void Render(HtmlTextWriter writer) {
194 ValidatorCollection validators;
197 // First, figure out if there's even data to deal with
199 validators = Page.GetValidators (ValidationGroup);
201 validators = Page.Validators;
203 if (validators.Count == 0) {
207 if (EnableClientScript && pre_render_called) {
208 Page.RegisterArrayDeclaration ("Page_ValidationSummaries",
209 String.Format ("document.getElementById ('{0}')", ID));
212 // We have validators
213 errors = new ArrayList(validators.Count);
214 for (int i = 0; i < validators.Count; i++) {
215 if (!validators[i].IsValid) {
216 errors.Add(validators[i].ErrorMessage);
220 if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
221 base.RenderBeginTag(writer);
223 if (ShowSummary && errors.Count > 0) {
224 switch(DisplayMode) {
225 case ValidationSummaryDisplayMode.BulletList: {
226 if (HeaderText.Length > 0) {
227 writer.Write(HeaderText);
230 writer.Write("<ul>");
231 for (int i = 0; i < errors.Count; i++) {
232 writer.Write("<li>");
233 writer.Write(errors[i]);
234 writer.Write("</li>");
236 writer.Write("</ul>");
240 case ValidationSummaryDisplayMode.List: {
241 if (HeaderText.Length > 0) {
242 writer.Write(HeaderText);
244 writer.Write("<br />");
246 writer.Write("<br>");
250 for (int i = 0; i < errors.Count; i++) {
251 writer.Write(errors[i]);
253 writer.Write("<br />");
255 writer.Write("<br>");
261 case ValidationSummaryDisplayMode.SingleParagraph: {
262 if (HeaderText.Length > 0) {
263 writer.Write(HeaderText);
267 for (int i = 0; i < errors.Count; i++) {
268 writer.Write(errors[i]);
272 writer.Write("<br />");
274 writer.Write("<br>");
282 if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
283 base.RenderEndTag(writer);
285 #endregion // Public Instance Methods