merge -r 58060:58217
[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.Collections;
28 using System.ComponentModel;
29 using System.Drawing;
30 using System.Globalization;
31 using System.Security.Permissions;
32
33 namespace System.Web.UI.WebControls {
34
35         // CAS
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 #if NET_2_0
39         [Designer ("System.Web.UI.Design.WebControls.PreviewControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40 #endif
41         public class ValidationSummary : WebControl {
42                 #region Public Constructors
43                 public ValidationSummary() : base(HtmlTextWriterTag.Div) {
44                         this.ForeColor = Color.Red;
45                 }
46                 #endregion      // Public Constructors
47
48                 #region Public Instance Properties
49 #if ONLY_1_1
50                 [Bindable(true)]
51 #endif          
52                 [DefaultValue(ValidationSummaryDisplayMode.BulletList)]
53                 [WebSysDescription ("")]
54                 [WebCategory ("Appearance")]
55                 public ValidationSummaryDisplayMode DisplayMode {
56                         get {
57                                 object obj;
58
59                                 obj = ViewState["DisplayMode"];
60                                 if (obj != null) {
61                                         return (ValidationSummaryDisplayMode)obj;
62                                 }
63                                 return ValidationSummaryDisplayMode.BulletList;
64                         }
65
66                         set {
67                                 ViewState["DisplayMode"] = value;
68                         }
69                 }
70
71                 [DefaultValue(true)]
72 #if NET_2_0
73                 [Themeable (false)]
74 #endif          
75                 [WebSysDescription ("")]
76                 [WebCategory ("Behavior")]
77                 public bool EnableClientScript {
78                         get {
79                                 return ViewState.GetBool("EnableClientScript", true);
80                         }
81
82                         set {
83                                 ViewState["EnableClientScript"] = value;
84                         }
85                 }
86
87                 [DefaultValue(typeof (Color), "Red")]
88                 public override System.Drawing.Color ForeColor {
89                         get {
90                                 return base.ForeColor;
91                         }
92
93                         set {
94                                 base.ForeColor = value;
95                         }
96                 }
97
98 #if ONLY_1_1
99                 [Bindable(true)]
100 #endif          
101                 [DefaultValue("")]
102 #if NET_2_0
103                 [Localizable (true)]
104 #endif          
105                 [WebSysDescription ("")]
106                 [WebCategory ("Appearance")]
107                 public string HeaderText {
108                         get {
109                                 return ViewState.GetString("HeaderText", string.Empty);
110                         }
111
112                         set {
113                                 ViewState["HeaderText"] = value;
114                         }
115                 }
116
117 #if ONLY_1_1
118                 [Bindable(true)]
119 #endif          
120                 [DefaultValue(false)]
121                 [WebSysDescription ("")]
122                 [WebCategory ("Behavior")]
123                 public bool ShowMessageBox {
124                         get {
125                                 return ViewState.GetBool("ShowMessageBox", false);
126                         }
127
128                         set {
129                                 ViewState["ShowMessageBox"] = value;
130                         }
131                 }
132
133 #if ONLY_1_1
134                 [Bindable(true)]
135 #endif          
136                 [DefaultValue(true)]
137                 [WebSysDescription ("")]
138                 [WebCategory ("Behavior")]
139                 public bool ShowSummary {
140                         get {
141                                 return ViewState.GetBool("ShowSummary", true);
142                         }
143
144                         set {
145                                 ViewState["ShowSummary"] = value;
146                         }
147                 }
148
149 #if NET_2_0
150                 [DefaultValue ("")]
151                 [Themeable (false)]
152                 public string ValidationGroup
153                 {
154                         get {
155                                 return ViewState.GetString("ValidationGroup", string.Empty);
156                         }
157                         set {
158                                 ViewState["ValidationGroup"] = value;
159                         }
160                 }
161 #endif          
162                 
163                 #endregion      // Public Instance Properties
164
165                 #region Public Instance Methods
166                 protected override void AddAttributesToRender(HtmlTextWriter writer) {
167                         base.AddAttributesToRender (writer);
168
169                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
170                                 /* force an ID here if we weren't assigned one */
171                                 if (ID == null)
172                                         writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
173
174                                 if (HeaderText != "")
175                                         writer.AddAttribute ("headertext", HeaderText);
176
177                                 if (ShowMessageBox)
178                                         writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
179
180                                 if (ShowSummary == false)
181                                         writer.AddAttribute ("showsummary", ShowSummary.ToString());
182
183                                 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
184                                         writer.AddAttribute ("displaymode", DisplayMode.ToString());
185
186                                 if (!has_errors)
187                                         writer.AddStyleAttribute ("display", "none");
188                         }
189                 }
190
191 #if NET_2_0
192                 protected internal
193 #else           
194                 protected
195 #endif          
196                 override void OnPreRender(EventArgs e) {
197                         base.OnPreRender (e);
198
199                         pre_render_called = true;
200                 }
201
202 #if NET_2_0
203                 protected internal
204 #else           
205                 protected
206 #endif          
207                 override void Render(HtmlTextWriter writer) {
208                         ValidatorCollection     validators;
209                         ArrayList               errors;
210
211                         // First, figure out if there's even data to deal with
212 #if NET_2_0
213                         validators = Page.GetValidators (ValidationGroup);
214 #else
215                         validators = Page.Validators;
216 #endif
217                         if (validators.Count == 0) {
218                                 return;
219                         }
220
221                         // We have validators
222                         errors = new ArrayList(validators.Count);
223                         for (int i = 0; i < validators.Count; i++) {
224                                 if (!validators[i].IsValid) {
225                                         errors.Add(validators[i].ErrorMessage);
226                                 }
227                         }
228
229                         has_errors = errors.Count > 0;
230
231                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
232                                 Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
233                                                                             String.Format ("document.getElementById ('{0}')", ClientID));
234                         }
235
236                         if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
237                                 base.RenderBeginTag(writer);
238
239                         if (ShowSummary && has_errors) {
240                                 switch(DisplayMode) {
241                                         case ValidationSummaryDisplayMode.BulletList: {
242                                                 if (HeaderText.Length > 0) {
243                                                         writer.Write(HeaderText);
244                                                 }
245
246                                                 writer.Write("<ul>");
247                                                 for (int i = 0; i < errors.Count; i++) {
248                                                         writer.Write("<li>");
249                                                         writer.Write(errors[i]);
250                                                         writer.Write("</li>");
251                                                 }
252                                                 writer.Write("</ul>");
253                                                 break;
254                                         }
255
256                                         case ValidationSummaryDisplayMode.List: {
257                                                 if (HeaderText.Length > 0) {
258                                                         writer.Write(HeaderText);
259 #if NET_2_0
260                                                         writer.Write("<br />");
261 #else
262                                                         writer.Write("<br>");
263 #endif
264                                                 }
265
266                                                 for (int i = 0; i < errors.Count; i++) {
267                                                         writer.Write(errors[i]);
268 #if NET_2_0
269                                                         writer.Write("<br />");
270 #else
271                                                         writer.Write("<br>");
272 #endif
273                                                 }
274                                                 break;
275                                         }
276
277                                         case ValidationSummaryDisplayMode.SingleParagraph: {
278                                                 if (HeaderText.Length > 0) {
279                                                         writer.Write(HeaderText);
280                                                         writer.Write(" ");
281                                                 }
282
283                                                 for (int i = 0; i < errors.Count; i++) {
284                                                         writer.Write(errors[i]);
285                                                         writer.Write(" ");
286                                                 }
287 #if NET_2_0
288                                                 writer.Write("<br />");
289 #else
290                                                 writer.Write("<br>");
291 #endif
292
293                                                 break;
294                                         }
295                                 }
296                         }
297
298                         if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
299                                 base.RenderEndTag(writer);
300                 }
301                 #endregion      // Public Instance Methods
302
303                 bool pre_render_called;
304                 bool has_errors;
305         }
306 }