2005-08-25 Peter Dennis Bartok <pbartok@novell.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                 public ValidationSummaryDisplayMode DisplayMode {
53                         get {
54                                 object obj;
55
56                                 obj = ViewState["DisplayMode"];
57                                 if (obj != null) {
58                                         return (ValidationSummaryDisplayMode)obj;
59                                 }
60                                 return ValidationSummaryDisplayMode.BulletList;
61                         }
62
63                         set {
64                                 ViewState["DisplayMode"] = value;
65                         }
66                 }
67
68                 [DefaultValue(true)]
69 #if NET_2_0
70                 [Themeable (false)]
71 #endif          
72                 public bool EnableClientScript {
73                         get {
74                                 return ViewState.GetBool("EnableClientScript", true);
75                         }
76
77                         set {
78                                 ViewState["EnableClientScript"] = value;
79                         }
80                 }
81
82                 [DefaultValue("Color [Red]")]
83                 public override System.Drawing.Color ForeColor {
84                         get {
85                                 return base.ForeColor;
86                         }
87
88                         set {
89                                 base.ForeColor = value;
90                         }
91                 }
92
93 #if ONLY_1_1
94                 [Bindable(true)]
95 #endif          
96                 [DefaultValue("")]
97 #if NET_2_0
98                 [Localizable (true)]
99 #endif          
100                 public string HeaderText {
101                         get {
102                                 return ViewState.GetString("HeaderText", string.Empty);
103                         }
104
105                         set {
106                                 ViewState["HeaderText"] = value;
107                         }
108                 }
109
110 #if ONLY_1_1
111                 [Bindable(true)]
112 #endif          
113                 [DefaultValue(false)]
114                 public bool ShowMessageBox {
115                         get {
116                                 return ViewState.GetBool("ShowMessageBox", false);
117                         }
118
119                         set {
120                                 ViewState["ShowMessageBox"] = value;
121                         }
122                 }
123
124 #if ONLY_1_1
125                 [Bindable(true)]
126 #endif          
127                 [DefaultValue(true)]
128                 public bool ShowSummary {
129                         get {
130                                 return ViewState.GetBool("ShowSummary", true);
131                         }
132
133                         set {
134                                 ViewState["ShowSummary"] = value;
135                         }
136                 }
137
138 #if NET_2_0
139                 [DefaultValue ("")]
140                 [Themeable (false)]
141                 public string ValidationGroup
142                 {
143                         get {
144                                 return ViewState.GetString("ValidationGroup", string.Empty);
145                         }
146                         set {
147                                 ViewState["ValidationGroup"] = value;
148                         }
149                 }
150 #endif          
151                 
152                 #endregion      // Public Instance Properties
153
154                 #region Public Instance Methods
155                 protected override void AddAttributesToRender(HtmlTextWriter writer) {
156                         base.AddAttributesToRender (writer);
157
158                         if (EnableClientScript && pre_render_called) {
159                                 if (HeaderText != "")
160                                         writer.AddAttribute ("headertext", HeaderText);
161
162                                 if (ShowMessageBox)
163                                         writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
164
165                                 if (ShowSummary == false)
166                                         writer.AddAttribute ("showsummary", ShowSummary.ToString());
167
168                                 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
169                                         writer.AddAttribute ("displaymode", DisplayMode.ToString());
170
171                                 writer.AddStyleAttribute ("display", "none");
172                         }
173                 }
174
175                 bool pre_render_called = false;
176
177 #if NET_2_0
178                 protected internal
179 #else           
180                 protected
181 #endif          
182                 override void OnPreRender(EventArgs e) {
183                         base.OnPreRender (e);
184
185                         pre_render_called = true;
186                 }
187
188 #if NET_2_0
189                 protected internal
190 #else           
191                 protected
192 #endif          
193                 override void Render(HtmlTextWriter writer) {
194                         ValidatorCollection     validators;
195                         ArrayList               errors;
196
197                         // First, figure out if there's even data to deal with
198 #if NET_2_0
199                         validators = Page.GetValidators (ValidationGroup);
200 #else
201                         validators = Page.Validators;
202 #endif
203                         if (validators.Count == 0) {
204                                 return;
205                         }
206
207                         if (EnableClientScript && pre_render_called) {
208                                 Page.RegisterArrayDeclaration ("Page_ValidationSummaries",
209                                                                String.Format ("document.getElementById ('{0}')", ID));
210                         }
211
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);
217                                 }
218                         }
219
220                         if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
221                                 base.RenderBeginTag(writer);
222
223                         if (ShowSummary && errors.Count > 0) {
224                                 switch(DisplayMode) {
225                                         case ValidationSummaryDisplayMode.BulletList: {
226                                                 if (HeaderText.Length > 0) {
227                                                         writer.Write(HeaderText);
228                                                 }
229
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>");
235                                                 }
236                                                 writer.Write("</ul>");
237                                                 break;
238                                         }
239
240                                         case ValidationSummaryDisplayMode.List: {
241                                                 if (HeaderText.Length > 0) {
242                                                         writer.Write(HeaderText);
243 #if NET_2_0
244                                                         writer.Write("<br />");
245 #else
246                                                         writer.Write("<br>");
247 #endif
248                                                 }
249
250                                                 for (int i = 0; i < errors.Count; i++) {
251                                                         writer.Write(errors[i]);
252 #if NET_2_0
253                                                         writer.Write("<br />");
254 #else
255                                                         writer.Write("<br>");
256 #endif
257                                                 }
258                                                 break;
259                                         }
260
261                                         case ValidationSummaryDisplayMode.SingleParagraph: {
262                                                 if (HeaderText.Length > 0) {
263                                                         writer.Write(HeaderText);
264                                                         writer.Write(" ");
265                                                 }
266
267                                                 for (int i = 0; i < errors.Count; i++) {
268                                                         writer.Write(errors[i]);
269                                                         writer.Write(" ");
270                                                 }
271 #if NET_2_0
272                                                 writer.Write("<br />");
273 #else
274                                                 writer.Write("<br>");
275 #endif
276
277                                                 break;
278                                         }
279                                 }
280                         }
281
282                         if ((ShowSummary && errors.Count > 0) || (EnableClientScript && pre_render_called))
283                                 base.RenderEndTag(writer);
284                 }
285                 #endregion      // Public Instance Methods
286         }
287 }