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 && Page.AreValidatorsUplevel ()) {
169 /* force an ID here if we weren't assigned one */
171 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
173 if (HeaderText != "")
174 writer.AddAttribute ("headertext", HeaderText);
177 writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
179 if (ShowSummary == false)
180 writer.AddAttribute ("showsummary", ShowSummary.ToString());
182 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
183 writer.AddAttribute ("displaymode", DisplayMode.ToString());
186 writer.AddStyleAttribute ("display", "none");
195 override void OnPreRender(EventArgs e) {
196 base.OnPreRender (e);
198 pre_render_called = true;
206 override void Render(HtmlTextWriter writer) {
207 ValidatorCollection validators;
210 // First, figure out if there's even data to deal with
212 validators = Page.GetValidators (ValidationGroup);
214 validators = Page.Validators;
216 if (validators.Count == 0) {
220 // We have validators
221 errors = new ArrayList(validators.Count);
222 for (int i = 0; i < validators.Count; i++) {
223 if (!validators[i].IsValid) {
224 errors.Add(validators[i].ErrorMessage);
228 has_errors = errors.Count > 0;
230 if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
231 Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
232 String.Format ("document.getElementById ('{0}')", ClientID));
235 if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
236 base.RenderBeginTag(writer);
238 if (ShowSummary && has_errors) {
239 switch(DisplayMode) {
240 case ValidationSummaryDisplayMode.BulletList: {
241 if (HeaderText.Length > 0) {
242 writer.Write(HeaderText);
245 writer.Write("<ul>");
246 for (int i = 0; i < errors.Count; i++) {
247 writer.Write("<li>");
248 writer.Write(errors[i]);
249 writer.Write("</li>");
251 writer.Write("</ul>");
255 case ValidationSummaryDisplayMode.List: {
256 if (HeaderText.Length > 0) {
257 writer.Write(HeaderText);
259 writer.Write("<br />");
261 writer.Write("<br>");
265 for (int i = 0; i < errors.Count; i++) {
266 writer.Write(errors[i]);
268 writer.Write("<br />");
270 writer.Write("<br>");
276 case ValidationSummaryDisplayMode.SingleParagraph: {
277 if (HeaderText.Length > 0) {
278 writer.Write(HeaderText);
282 for (int i = 0; i < errors.Count; i++) {
283 writer.Write(errors[i]);
287 writer.Write("<br />");
289 writer.Write("<br>");
297 if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
298 base.RenderEndTag(writer);
300 #endregion // Public Instance Methods
302 bool pre_render_called;