merge -r 98047:98048
[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 virtual 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                 [MonoTODO ()]
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);
171
172 #if NET_2_0
173                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
174 #else
175                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
176 #endif
177                                 /* force an ID here if we weren't assigned one */
178                                 if (ID == null)
179                                         writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
180 #if NET_2_0
181                                 if (ValidationGroup != String.Empty)
182                                         RegisterExpandoAttribute (ClientID, "validationGroup", ValidationGroup);
183
184                                 if (HeaderText.Length > 0)
185                                         RegisterExpandoAttribute (ClientID, "headertext", HeaderText);
186
187                                 if (ShowMessageBox)
188                                         RegisterExpandoAttribute (ClientID, "showmessagebox", "True");
189
190                                 if (!ShowSummary)
191                                         RegisterExpandoAttribute (ClientID, "showsummary", "False");
192
193                                 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
194                                         RegisterExpandoAttribute (ClientID, "displaymode", DisplayMode.ToString ());
195 #else
196                                 if (HeaderText != "")
197                                         writer.AddAttribute ("headertext", HeaderText);
198
199                                 if (ShowMessageBox)
200                                         writer.AddAttribute ("showmessagebox", ShowMessageBox.ToString());
201
202                                 if (ShowSummary == false)
203                                         writer.AddAttribute ("showsummary", ShowSummary.ToString());
204
205                                 if (DisplayMode != ValidationSummaryDisplayMode.BulletList)
206                                         writer.AddAttribute ("displaymode", DisplayMode.ToString());
207 #endif
208
209                                 if (!has_errors)
210                                         writer.AddStyleAttribute ("display", "none");
211                         }
212                 }
213
214 #if NET_2_0
215                 internal void RegisterExpandoAttribute (string controlId, string attributeName, string attributeValue) {
216                         RegisterExpandoAttribute (controlId, attributeName, attributeValue, false);
217                 }
218
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);
222                         else
223                                 Page.ClientScript.RegisterExpandoAttribute (controlId, attributeName, attributeValue, encode);
224                 }
225 #endif
226
227 #if NET_2_0
228                 protected internal
229 #else           
230                 protected
231 #endif          
232                 override void OnPreRender(EventArgs e) {
233                         base.OnPreRender (e);
234
235                         pre_render_called = true;
236                 }
237
238 #if NET_2_0
239                 protected internal
240 #else           
241                 protected
242 #endif          
243                 override void Render(HtmlTextWriter writer) {
244 #if NET_2_0
245                         if (!Enabled)
246                                 return;
247 #endif
248                         ValidatorCollection     validators;
249                         ArrayList               errors;
250
251                         // First, figure out if there's even data to deal with
252 #if NET_2_0
253                         validators = Page.GetValidators (ValidationGroup);
254 #else
255                         validators = Page.Validators;
256 #endif
257
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);
263                                 }
264                         }
265
266                         has_errors = errors.Count > 0;
267
268 #if NET_2_0
269                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel (ValidationGroup)) {
270 #else
271                         if (EnableClientScript && pre_render_called && Page.AreValidatorsUplevel ()) {
272 #endif
273 #if NET_2_0
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",
277 @"
278 document.getElementById('" + ClientID + @"').dispose = function() {
279         Array.remove(Page_ValidationSummaries, document.getElementById('" + ClientID + @"'));
280 }
281 ", true);
282                                         }
283                                 else
284 #endif
285                                 Page.ClientScript.RegisterArrayDeclaration ("Page_ValidationSummaries",
286                                                                             String.Concat ("document.getElementById ('", ClientID, "')"));
287                         }
288
289                         if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
290                                 base.RenderBeginTag(writer);
291
292                         if (ShowSummary && has_errors) {
293                                 switch(DisplayMode) {
294                                         case ValidationSummaryDisplayMode.BulletList: {
295                                                 if (HeaderText.Length > 0) {
296                                                         writer.Write(HeaderText);
297                                                 }
298
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>");
304                                                 }
305                                                 writer.Write("</ul>");
306                                                 break;
307                                         }
308
309                                         case ValidationSummaryDisplayMode.List: {
310                                                 if (HeaderText.Length > 0) {
311                                                         writer.Write(HeaderText);
312 #if NET_2_0
313                                                         writer.Write("<br />");
314 #else
315                                                         writer.Write("<br>");
316 #endif
317                                                 }
318
319                                                 for (int i = 0; i < errors.Count; i++) {
320                                                         writer.Write(errors[i]);
321 #if NET_2_0
322                                                         writer.Write("<br />");
323 #else
324                                                         writer.Write("<br>");
325 #endif
326                                                 }
327                                                 break;
328                                         }
329
330                                         case ValidationSummaryDisplayMode.SingleParagraph: {
331                                                 if (HeaderText.Length > 0) {
332                                                         writer.Write(HeaderText);
333                                                         writer.Write(" ");
334                                                 }
335
336                                                 for (int i = 0; i < errors.Count; i++) {
337                                                         writer.Write(errors[i]);
338                                                         writer.Write(" ");
339                                                 }
340 #if NET_2_0
341                                                 writer.Write("<br />");
342 #else
343                                                 writer.Write("<br>");
344 #endif
345
346                                                 break;
347                                         }
348                                 }
349                         }
350
351                         if ((ShowSummary && has_errors) || (EnableClientScript && pre_render_called))
352                                 base.RenderEndTag(writer);
353                 }
354                 #endregion      // Public Instance Methods
355
356                 bool pre_render_called;
357                 bool has_errors;
358         }
359 }