updating to the latest module.
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / ValidationSummary.cs
1 //
2 // System.Web.UI.WebControls.ValidationSummary.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 //
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
10 //\r
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 \r
33 using System;
34 using System.ComponentModel;\r
35 using System.Drawing;\r
36 using System.Web;\r
37 using System.Web.UI;\r
38 \r
39 namespace System.Web.UI.WebControls\r
40 {\r
41         public class ValidationSummary : WebControl\r
42         {\r
43                 private bool uplevelRender;\r
44 \r
45                 public ValidationSummary(): base(HtmlTextWriterTag.Div)\r
46                 {\r
47                         uplevelRender = false;\r
48                         ForeColor     = Color.Red;\r
49                 }\r
50
51 #if !NET_2_0
52                 [Bindable (true)]
53 #endif
54                 [DefaultValue (typeof (ValidationSummaryDisplayMode), "BulletList"), WebCategory ("Appearance")]
55                 [WebSysDescription ("The type of how validation summaries should be displayed.")]\r
56                 public ValidationSummaryDisplayMode DisplayMode\r
57                 {\r
58                         get\r
59                         {\r
60                                 object o = ViewState["DisplayMode"];\r
61                                 if(o != null)\r
62                                         return (ValidationSummaryDisplayMode)o;\r
63                                 return ValidationSummaryDisplayMode.BulletList;\r
64                         }\r
65                         set\r
66                         {\r
67                                 if(!Enum.IsDefined(typeof(ValidationSummaryDisplayMode), value))\r
68                                         throw new ArgumentException();\r
69                                 ViewState["DisplayMode"] = value;\r
70                         }\r
71                 }\r
72
73 #if NET_2_0
74             [ThemeableAttribute (false)]
75 #endif
76                 [DefaultValue (true), WebCategory ("Behavior")]
77                 [WebSysDescription ("Determines if the validation summary should be updated directly on the client using script code.")]\r
78                 public bool EnableClientScript\r
79                 {\r
80                         get\r
81                         {\r
82                                 object o = ViewState["EnableClientScript"];\r
83                                 if(o != null)\r
84                                         return (bool)o;\r
85                                 return true;\r
86                         }\r
87                         set\r
88                         {\r
89                                 ViewState["EnableClientScript"] = value;\r
90                         }\r
91                 }\r
92
93                 [DefaultValue (null)]\r
94                 public override Color ForeColor\r
95                 {\r
96                         get\r
97                         {\r
98                                 return base.ForeColor;\r
99                         }\r
100                         set\r
101                         {\r
102                                 base.ForeColor = value;\r
103                         }\r
104                 }\r
105
106 #if !NET_2_0
107                 [Bindable (true)]
108 #endif
109                 [DefaultValue (false), WebCategory ("Behavior")]
110                 [WebSysDescription ("Determines if the validation summary should display a message box on the client if an uplevel browser is used.")]\r
111                 public bool ShowMessageBox\r
112                 {\r
113                         get\r
114                         {\r
115                                 object o = ViewState["ShowMessageBox"];\r
116                                 if(o != null)\r
117                                         return (bool)o;\r
118                                 return false;\r
119                         }\r
120                         set\r
121                         {\r
122                                 ViewState["ShowMessageBox"] = value;\r
123                         }\r
124                 }\r
125
126 #if !NET_2_0
127                 [Bindable (true)]
128 #endif
129                 [DefaultValue (true), WebCategory ("Behavior")]
130                 [WebSysDescription ("Determines if the validation summary should display a summary.")]\r
131                 public bool ShowSummary\r
132                 {\r
133                         get\r
134                         {\r
135                                 object o = ViewState["ShowSummary"];\r
136                                 if(o != null)\r
137                                         return (bool)o;\r
138                                 return true;\r
139                         }\r
140                         set\r
141                         {\r
142                                 ViewState["ShowSummary"] = value;\r
143                         }\r
144                 }\r
145
146 #if NET_2_0
147                 [Localizable (true)]
148 #endif
149                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
150                 [WebSysDescription ("A text that is diplayed as a header for the validation report.")]\r
151                 public string HeaderText\r
152                 {\r
153                         get\r
154                         {\r
155                                 object o = ViewState["HeaderText"];\r
156                                 if(o != null)\r
157                                         return (string)o;\r
158                                 return String.Empty;\r
159                         }\r
160                         set\r
161                         {\r
162                                 ViewState["HeaderText"] = value;\r
163                         }\r
164                 }\r
165 \r
166                 protected override void AddAttributesToRender(HtmlTextWriter writer)\r
167                 {\r
168                         base.AddAttributesToRender(writer);\r
169                         if(uplevelRender)\r
170                         {\r
171                                 if(ID == null || ID.Length == 0)\r
172                                         writer.AddAttribute("id", ClientID);\r
173                                 if(HeaderText.Length > 0)\r
174                                         writer.AddAttribute("headertext", HeaderText, true);\r
175                                 if(ShowMessageBox)\r
176                                         writer.AddAttribute("showmessagebox", "True");\r
177                                 if(!ShowSummary)\r
178                                         writer.AddAttribute("showsummary", "False");\r
179                                 if(DisplayMode != ValidationSummaryDisplayMode.BulletList)\r
180                                 {\r
181                                         writer.AddAttribute("displaymode", PropertyConverter.EnumToString(typeof(ValidationSummaryDisplayMode), DisplayMode));\r
182                                 }\r
183                         }\r
184                 }
185                 
186                 protected override void OnPreRender(EventArgs e)
187                 {
188                 }
189                 
190                 protected override void Render(HtmlTextWriter writer)
191                 {
192                         if (!base.Enabled) return;
193         
194                         string[] messages;
195                         bool toDisplay;
196                         bool showSummary;
197         
198                         if (base.Site != null && base.Site.DesignMode)
199                         {
200                                 showSummary = true;
201                                 messages = new string[]{HttpRuntime.FormatResourceString("ValSummary_error_message_1"),
202                                                         HttpRuntime.FormatResourceString("ValSummary_error_message_2")};
203                                 toDisplay = true;
204                                 uplevelRender = false;
205                         }
206                         else
207                         {
208                                 showSummary = false;
209                                 messages = null;
210         
211                                 //Messages count
212                                 int numOfMsg = 0;
213                                 for (int i = 0; i < base.Page.Validators.Count; i++)
214                                 {
215                                         IValidator currentValidator = base.Page.Validators[i];
216                                         if (!currentValidator.IsValid)
217                                         {
218                                                 showSummary = true;
219                                                 if (currentValidator.ErrorMessage != null &&
220                                                     currentValidator.ErrorMessage.Length != 0)
221                                                         numOfMsg++;
222                                         }
223                                 }
224         
225                                 if (numOfMsg != 0)
226                                 {
227                                         messages = new string[(int)numOfMsg];
228                                         int cur_msg = 0;
229                                         for (int i = 0; i < base.Page.Validators.Count; i++)
230                                         {
231                                                 IValidator currentValidator = base.Page.Validators[i];
232                                                 if (!currentValidator.IsValid &&
233                                                      currentValidator.ErrorMessage != null &&
234                                                      currentValidator.ErrorMessage.Length != 0) 
235                                                         messages[cur_msg++] = String.Copy(currentValidator.ErrorMessage);
236                                         }
237                                 }
238         
239                                 toDisplay = ShowSummary ? showSummary : false;
240                                 if (!toDisplay && uplevelRender)
241                                         base.Style["display"] = "none";
242                         }
243                         if (base.Page != null)
244                         {
245                                 base.Page.VerifyRenderingInServerForm(this);
246                         }
247                         bool tagRequired = !uplevelRender ? toDisplay : true;
248                         if (tagRequired)
249                         {
250                                 base.RenderBeginTag(writer);
251                         }
252                         if (toDisplay)
253                         {
254                                 string str1, str2, str3, str4, str5;
255                                
256         
257                                 switch (DisplayMode)
258                                 {
259                                 case ValidationSummaryDisplayMode.List:
260                                         str1 = "<br>";
261                                         str2 = "";
262                                         str3 = "";
263                                         str4 = "<br>";
264                                         str5 = "";
265                                         break;
266                                         
267                                 case ValidationSummaryDisplayMode.SingleParagraph:
268                                         str1 = " ";
269                                         str2 = "";
270                                         str3 = "";
271                                         str4 = " ";
272                                         str5 = "<br>";
273                                         break;
274                                         
275                                 default:
276                                         str1 = "";
277                                         str2 = "<ul>";
278                                         str3 = "<li>";
279                                         str4 = "</li>";
280                                         str5 = "</ul>";
281                                         break;
282                                 }
283                                 if (HeaderText.Length > 0)
284                                 {
285                                         writer.Write(HeaderText);
286                                         writer.Write(str1);
287                                 }
288                                 writer.Write(str2);
289                                 if (messages != null)
290                                 {
291                                         for (int i = 0; i < (int)messages.Length; i++)
292                                         {
293                                                 writer.Write(str3);
294                                                 writer.Write(messages[i]);
295                                                 writer.Write(str4);
296                                         }
297                                 }
298                                 writer.Write(str5);
299                         }
300                         if (tagRequired)
301                         {
302                                 base.RenderEndTag(writer);
303                         }
304                 }
305         }
306 }\r