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 virtual string ValidationGroup
155 return ViewState.GetString("ValidationGroup", string.Empty);
158 ViewState["ValidationGroup"] = value;
163 #endregion // Public Instance Properties
165 #region Public Instance Methods
167 // for 2.0: not XHTML attributes must be registered with RegisterExpandoAttribute
168 // when it will be implemented, in this case WebUIValidation_2.0.js muist be refactored
169 protected override void AddAttributesToRender(HtmlTextWriter writer) {
170 base.AddAttributesToRender (writer);
173 if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
175 if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
177 /* force an ID here if we weren't assigned one */
179 writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
181 if (ValidationGroup != String.Empty)
182 RegisterExpandoAttribute (ClientID, "validationGroup", ValidationGroup);
184 if (HeaderText.Length > 0)
185 RegisterExpandoAttribute (ClientID, "headertext", HeaderText);
188 RegisterExpandoAttribute (ClientID, "showmessagebox", "True");
191 RegisterExpandoAttribute (ClientID, "showsummary", "False");
193 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
194 RegisterExpandoAttribute (ClientID, "displaymode", DisplayMode.ToString ());
196 if (HeaderText != "")
197 writer.AddAttribute ("headertext", HeaderText);
200 writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
202 if (ShowSummary == false)
203 writer.AddAttribute ("showsummary", ShowSummary.ToString());
205 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
206 writer.AddAttribute ("displaymode", DisplayMode.ToString());
210 writer.AddStyleAttribute ("display", "none");
215 internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue) {
216 RegisterExpandoAttribute (controlId, attributeName, attributeValue, false);
219 internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue, bool encode) {
220 if (Page.ScriptManager != null)
221 Page.ScriptManager.RegisterExpandoAttributeExternal (this, controlId, attributeName, attributeValue, encode);
223 Page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
232 override void OnPreRender(EventArgs e) {
233 base.OnPreRender (e);
235 pre_render_called = true;
243 override void Render(HtmlTextWriter writer) {
248 ValidatorCollection validators;
251 // First, figure out if there's even data to deal with
253 validators = Page.GetValidators (ValidationGroup);
255 validators = Page.Validators;
258 // We have validators
259 errors = new ArrayList(validators.Count);
260 for (int i = 0; i < validators.Count; i++) {
261 if (!validators[i].IsValid) {
262 errors.Add(validators[i].ErrorMessage);
266 has_errors = errors.Count > 0;
269 if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
271 if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
274 if (Page.ScriptManager != null) {
275 Page.ScriptManager.RegisterArrayDeclarationExternal (this, "Page_ValidationSummaries", String.Concat ("document.getElementById ('", ClientID, "')"));
276 Page.ScriptManager.RegisterStartupScriptExternal (this, typeof (BaseValidator), ClientID + "DisposeScript",
278 document.getElementById('" + ClientID + @"').dispose = function() {
279 Array.remove(Page_ValidationSummaries, document.getElementById('" + ClientID + @"'));
285 Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
286 String.Concat ("document.getElementById ('", ClientID, "')"));
289 if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
290 base.RenderBeginTag(writer);
292 if (ShowSummary && has_errors) {
293 switch(DisplayMode) {
294 case ValidationSummaryDisplayMode.BulletList: {
295 if (HeaderText.Length > 0) {
296 writer.Write(HeaderText);
299 writer.Write("<ul>");
300 for (int i = 0; i < errors.Count; i++) {
301 writer.Write("<li>");
302 writer.Write(errors[i]);
303 writer.Write("</li>");
305 writer.Write("</ul>");
309 case ValidationSummaryDisplayMode.List: {
310 if (HeaderText.Length > 0) {
311 writer.Write(HeaderText);
313 writer.Write("<br />");
315 writer.Write("<br>");
319 for (int i = 0; i < errors.Count; i++) {
320 writer.Write(errors[i]);
322 writer.Write("<br />");
324 writer.Write("<br>");
330 case ValidationSummaryDisplayMode.SingleParagraph: {
331 if (HeaderText.Length > 0) {
332 writer.Write(HeaderText);
336 for (int i = 0; i < errors.Count; i++) {
337 writer.Write(errors[i]);
341 writer.Write("<br />");
343 writer.Write("<br>");
351 if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
352 base.RenderEndTag(writer);
354 #endregion // Public Instance Methods
356 bool pre_render_called;