svn path=/branches/mono-1-1-9/mcs/; revision=51212
[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 && Page.AreValidatorsUplevel ()) {
169                                 /* force an ID here if we weren't assigned one */
170                                 if (ID == null)
171                                         writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
172
173                                 if (HeaderText != "")
174                                         writer.AddAttribute ("headertext", HeaderText);
175
176                                 if (ShowMessageBox)
177                                         writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
178
179                                 if (ShowSummary == false)
180                                         writer.AddAttribute ("showsummary", ShowSummary.ToString());
181
182                                 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
183                                         writer.AddAttribute ("displaymode", DisplayMode.ToString());
184
185                                 if (!has_errors)
186                                         writer.AddStyleAttribute ("display", "none");
187                         }
188                 }
189
190 #if NET_2_0
191                 protected internal
192 #else           
193                 protected
194 #endif          
195                 override void OnPreRender(EventArgs e) {
196                         base.OnPreRender (e);
197
198                         pre_render_called = true;
199                 }
200
201 #if NET_2_0
202                 protected internal
203 #else           
204                 protected
205 #endif          
206                 override void Render(HtmlTextWriter writer) {
207                         ValidatorCollection     validators;
208                         ArrayList               errors;
209
210                         // First, figure out if there's even data to deal with
211 #if NET_2_0
212                         validators = Page.GetValidators (ValidationGroup);
213 #else
214                         validators = Page.Validators;
215 #endif
216                         if (validators.Count == 0) {
217                                 return;
218                         }
219
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);
225                                 }
226                         }
227
228                         has_errors = errors.Count > 0;
229
230                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
231                                 Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
232                                                                             String.Format ("document.getElementById ('{0}')", ClientID));
233                         }
234
235                         if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
236                                 base.RenderBeginTag(writer);
237
238                         if (ShowSummary && has_errors) {
239                                 switch(DisplayMode) {
240                                         case ValidationSummaryDisplayMode.BulletList: {
241                                                 if (HeaderText.Length > 0) {
242                                                         writer.Write(HeaderText);
243                                                 }
244
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>");
250                                                 }
251                                                 writer.Write("</ul>");
252                                                 break;
253                                         }
254
255                                         case ValidationSummaryDisplayMode.List: {
256                                                 if (HeaderText.Length > 0) {
257                                                         writer.Write(HeaderText);
258 #if NET_2_0
259                                                         writer.Write("<br />");
260 #else
261                                                         writer.Write("<br>");
262 #endif
263                                                 }
264
265                                                 for (int i = 0; i < errors.Count; i++) {
266                                                         writer.Write(errors[i]);
267 #if NET_2_0
268                                                         writer.Write("<br />");
269 #else
270                                                         writer.Write("<br>");
271 #endif
272                                                 }
273                                                 break;
274                                         }
275
276                                         case ValidationSummaryDisplayMode.SingleParagraph: {
277                                                 if (HeaderText.Length > 0) {
278                                                         writer.Write(HeaderText);
279                                                         writer.Write(" ");
280                                                 }
281
282                                                 for (int i = 0; i < errors.Count; i++) {
283                                                         writer.Write(errors[i]);
284                                                         writer.Write(" ");
285                                                 }
286 #if NET_2_0
287                                                 writer.Write("<br />");
288 #else
289                                                 writer.Write("<br>");
290 #endif
291
292                                                 break;
293                                         }
294                                 }
295                         }
296
297                         if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
298                                 base.RenderEndTag(writer);
299                 }
300                 #endregion      // Public Instance Methods
301
302                 bool pre_render_called;
303                 bool has_errors;
304         }
305 }