2005-09-01 Chris Toshok <toshok@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ValidationSummary.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    (pbartok@novell.com)
24 //
25 //
26
27 using System;
28 using System.Collections;
29 using System.ComponentModel;
30 using System.Drawing;
31 using System.Globalization;
32 using System.Web;
33 using System.Web.UI;
34 using System.Web.UI.WebControls;
35
36 namespace System.Web.UI.WebControls {
37 #if NET_2_0
38         [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
39 #endif
40         public class ValidationSummary : WebControl {
41                 #region Public Constructors
42                 public ValidationSummary() : base(HtmlTextWriterTag.Div) {
43                         this.ForeColor = Color.Red;
44                 }
45                 #endregion      // Public Constructors
46
47                 #region Public Instance Properties
48 #if ONLY_1_1
49                 [Bindable(true)]
50 #endif          
51                 [DefaultValue(ValidationSummaryDisplayMode.BulletList)]
52                 [WebSysDescription ("")]
53                 [WebCategory ("Appearance")]
54                 public ValidationSummaryDisplayMode DisplayMode {
55                         get {
56                                 object obj;
57
58                                 obj = ViewState["DisplayMode"];
59                                 if (obj != null) {
60                                         return (ValidationSummaryDisplayMode)obj;
61                                 }
62                                 return ValidationSummaryDisplayMode.BulletList;
63                         }
64
65                         set {
66                                 ViewState["DisplayMode"] = value;
67                         }
68                 }
69
70                 [DefaultValue(true)]
71 #if NET_2_0
72                 [Themeable (false)]
73 #endif          
74                 [WebSysDescription ("")]
75                 [WebCategory ("Behavior")]
76                 public bool EnableClientScript {
77                         get {
78                                 return ViewState.GetBool("EnableClientScript", true);
79                         }
80
81                         set {
82                                 ViewState["EnableClientScript"] = value;
83                         }
84                 }
85
86                 [DefaultValue(typeof (Color), "Red")]
87                 public override System.Drawing.Color ForeColor {
88                         get {
89                                 return base.ForeColor;
90                         }
91
92                         set {
93                                 base.ForeColor = value;
94                         }
95                 }
96
97 #if ONLY_1_1
98                 [Bindable(true)]
99 #endif          
100                 [DefaultValue("")]
101 #if NET_2_0
102                 [Localizable (true)]
103 #endif          
104                 [WebSysDescription ("")]
105                 [WebCategory ("Appearance")]
106                 public string HeaderText {
107                         get {
108                                 return ViewState.GetString("HeaderText", string.Empty);
109                         }
110
111                         set {
112                                 ViewState["HeaderText"] = value;
113                         }
114                 }
115
116 #if ONLY_1_1
117                 [Bindable(true)]
118 #endif          
119                 [DefaultValue(false)]
120                 [WebSysDescription ("")]
121                 [WebCategory ("Behavior")]
122                 public bool ShowMessageBox {
123                         get {
124                                 return ViewState.GetBool("ShowMessageBox", false);
125                         }
126
127                         set {
128                                 ViewState["ShowMessageBox"] = value;
129                         }
130                 }
131
132 #if ONLY_1_1
133                 [Bindable(true)]
134 #endif          
135                 [DefaultValue(true)]
136                 [WebSysDescription ("")]
137                 [WebCategory ("Behavior")]
138                 public bool ShowSummary {
139                         get {
140                                 return ViewState.GetBool("ShowSummary", true);
141                         }
142
143                         set {
144                                 ViewState["ShowSummary"] = value;
145                         }
146                 }
147
148 #if NET_2_0
149                 [DefaultValue ("")]
150                 [Themeable (false)]
151                 public string ValidationGroup
152                 {
153                         get {
154                                 return ViewState.GetString("ValidationGroup", string.Empty);
155                         }
156                         set {
157                                 ViewState["ValidationGroup"] = value;
158                         }
159                 }
160 #endif          
161                 
162                 #endregion      // Public Instance Properties
163
164                 #region Public Instance Methods
165                 protected override void AddAttributesToRender(HtmlTextWriter writer) {
166                         base.AddAttributesToRender (writer);
167
168                         if (EnableClientScript && pre_render_called) {
169                                 if (HeaderText != "")
170                                         writer.AddAttribute ("headertext", HeaderText);
171
172                                 if (ShowMessageBox)
173                                         writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
174
175                                 if (ShowSummary == false)
176                                         writer.AddAttribute ("showsummary", ShowSummary.ToString());
177
178                                 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
179                                         writer.AddAttribute ("displaymode", DisplayMode.ToString());
180
181                                 writer.AddStyleAttribute ("display", "none");
182                         }
183                 }
184
185                 bool pre_render_called = false;
186
187 #if NET_2_0
188                 protected internal
189 #else           
190                 protected
191 #endif          
192                 override void OnPreRender(EventArgs e) {
193                         base.OnPreRender (e);
194
195                         pre_render_called = true;
196                 }
197
198 #if NET_2_0
199                 protected internal
200 #else           
201                 protected
202 #endif          
203                 override void Render(HtmlTextWriter writer) {
204                         ValidatorCollection     validators;
205                         ArrayList               errors;
206
207                         // First, figure out if there's even data to deal with
208 #if NET_2_0
209                         validators = Page.GetValidators (ValidationGroup);
210 #else
211                         validators = Page.Validators;
212 #endif
213                         if (validators.Count == 0) {
214                                 return;
215                         }
216
217                         if (EnableClientScript && pre_render_called) {
218                                 Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
219                                                                             String.Format ("document.getElementById ('{0}')", ID));
220                         }
221
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);
227                                 }
228                         }
229
230                         if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
231                                 base.RenderBeginTag(writer);
232
233                         if (ShowSummary && errors.Count > 0) {
234                                 switch(DisplayMode) {
235                                         case ValidationSummaryDisplayMode.BulletList: {
236                                                 if (HeaderText.Length > 0) {
237                                                         writer.Write(HeaderText);
238                                                 }
239
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>");
245                                                 }
246                                                 writer.Write("</ul>");
247                                                 break;
248                                         }
249
250                                         case ValidationSummaryDisplayMode.List: {
251                                                 if (HeaderText.Length > 0) {
252                                                         writer.Write(HeaderText);
253 #if NET_2_0
254                                                         writer.Write("<br />");
255 #else
256                                                         writer.Write("<br>");
257 #endif
258                                                 }
259
260                                                 for (int i = 0; i < errors.Count; i++) {
261                                                         writer.Write(errors[i]);
262 #if NET_2_0
263                                                         writer.Write("<br />");
264 #else
265                                                         writer.Write("<br>");
266 #endif
267                                                 }
268                                                 break;
269                                         }
270
271                                         case ValidationSummaryDisplayMode.SingleParagraph: {
272                                                 if (HeaderText.Length > 0) {
273                                                         writer.Write(HeaderText);
274                                                         writer.Write(" ");
275                                                 }
276
277                                                 for (int i = 0; i < errors.Count; i++) {
278                                                         writer.Write(errors[i]);
279                                                         writer.Write(" ");
280                                                 }
281 #if NET_2_0
282                                                 writer.Write("<br />");
283 #else
284                                                 writer.Write("<br>");
285 #endif
286
287                                                 break;
288                                         }
289                                 }
290                         }
291
292                         if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
293                                 base.RenderEndTag(writer);
294                 }
295                 #endregion      // Public Instance Methods
296         }
297 }