2004-06-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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 \r
12 using System;
13 using System.ComponentModel;\r
14 using System.Drawing;\r
15 using System.Web;\r
16 using System.Web.UI;\r
17 \r
18 namespace System.Web.UI.WebControls\r
19 {\r
20         public class ValidationSummary : WebControl\r
21         {\r
22                 private bool uplevelRender;\r
23 \r
24                 public ValidationSummary(): base(HtmlTextWriterTag.Div)\r
25                 {\r
26                         uplevelRender = false;\r
27                         ForeColor     = Color.Red;\r
28                 }\r
29
30                 [DefaultValue (typeof (ValidationSummaryDisplayMode), "BulletList"), Bindable (true), WebCategory ("Appearance")]
31                 [WebSysDescription ("The type of how validation summaries should be displayed.")]\r
32                 public ValidationSummaryDisplayMode DisplayMode\r
33                 {\r
34                         get\r
35                         {\r
36                                 object o = ViewState["DisplayMode"];\r
37                                 if(o != null)\r
38                                         return (ValidationSummaryDisplayMode)o;\r
39                                 return ValidationSummaryDisplayMode.BulletList;\r
40                         }\r
41                         set\r
42                         {\r
43                                 if(!Enum.IsDefined(typeof(ValidationSummaryDisplayMode), value))\r
44                                         throw new ArgumentException();\r
45                                 ViewState["DisplayMode"] = value;\r
46                         }\r
47                 }\r
48
49                 [DefaultValue (true), WebCategory ("Behavior")]
50                 [WebSysDescription ("Determines if the validation summary should be updated directly on the client using script code.")]\r
51                 public bool EnableClientScript\r
52                 {\r
53                         get\r
54                         {\r
55                                 object o = ViewState["EnableClientScript"];\r
56                                 if(o != null)\r
57                                         return (bool)o;\r
58                                 return true;\r
59                         }\r
60                         set\r
61                         {\r
62                                 ViewState["EnableClientScript"] = value;\r
63                         }\r
64                 }\r
65
66                 [DefaultValue (null)]\r
67                 public override Color ForeColor\r
68                 {\r
69                         get\r
70                         {\r
71                                 return base.ForeColor;\r
72                         }\r
73                         set\r
74                         {\r
75                                 base.ForeColor = value;\r
76                         }\r
77                 }\r
78
79                 [DefaultValue (false), Bindable (true), WebCategory ("Behavior")]
80                 [WebSysDescription ("Determines if the validation summary should display a message box on the client if an uplevel browser is used.")]\r
81                 public bool ShowMessageBox\r
82                 {\r
83                         get\r
84                         {\r
85                                 object o = ViewState["ShowMessageBox"];\r
86                                 if(o != null)\r
87                                         return (bool)o;\r
88                                 return false;\r
89                         }\r
90                         set\r
91                         {\r
92                                 ViewState["ShowMessageBox"] = value;\r
93                         }\r
94                 }\r
95
96                 [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
97                 [WebSysDescription ("Determines if the validation summary should display a summary.")]\r
98                 public bool ShowSummary\r
99                 {\r
100                         get\r
101                         {\r
102                                 object o = ViewState["ShowSummary"];\r
103                                 if(o != null)\r
104                                         return (bool)o;\r
105                                 return true;\r
106                         }\r
107                         set\r
108                         {\r
109                                 ViewState["ShowSummary"] = value;\r
110                         }\r
111                 }\r
112
113                 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
114                 [WebSysDescription ("A text that is diplayed as a header for the validation report.")]\r
115                 public string HeaderText\r
116                 {\r
117                         get\r
118                         {\r
119                                 object o = ViewState["HeaderText"];\r
120                                 if(o != null)\r
121                                         return (string)o;\r
122                                 return String.Empty;\r
123                         }\r
124                         set\r
125                         {\r
126                                 ViewState["HeaderText"] = value;\r
127                         }\r
128                 }\r
129 \r
130                 protected override void AddAttributesToRender(HtmlTextWriter writer)\r
131                 {\r
132                         base.AddAttributesToRender(writer);\r
133                         if(uplevelRender)\r
134                         {\r
135                                 if(ID == null || ID.Length == 0)\r
136                                         writer.AddAttribute("id", ClientID);\r
137                                 if(HeaderText.Length > 0)\r
138                                         writer.AddAttribute("headertext", HeaderText, true);\r
139                                 if(ShowMessageBox)\r
140                                         writer.AddAttribute("showmessagebox", "True");\r
141                                 if(!ShowSummary)\r
142                                         writer.AddAttribute("showsummary", "False");\r
143                                 if(DisplayMode != ValidationSummaryDisplayMode.BulletList)\r
144                                 {\r
145                                         writer.AddAttribute("displaymode", PropertyConverter.EnumToString(typeof(ValidationSummaryDisplayMode), DisplayMode));\r
146                                 }\r
147                         }\r
148                 }
149                 
150                 protected override void OnPreRender(EventArgs e)
151                 {
152                 }
153                 
154                 protected override void Render(HtmlTextWriter writer)
155                 {
156                         if (!base.Enabled) return;
157         
158                         string[] messages;
159                         bool toDisplay;
160                         bool showSummary;
161         
162                         if (base.Site != null && base.Site.DesignMode)
163                         {
164                                 showSummary = true;
165                                 messages = new string[]{HttpRuntime.FormatResourceString("ValSummary_error_message_1"),
166                                                         HttpRuntime.FormatResourceString("ValSummary_error_message_2")};
167                                 toDisplay = true;
168                                 uplevelRender = false;
169                         }
170                         else
171                         {
172                                 showSummary = false;
173                                 messages = null;
174         
175                                 //Messages count
176                                 int numOfMsg = 0;
177                                 for (int i = 0; i < base.Page.Validators.Count; i++)
178                                 {
179                                         IValidator currentValidator = base.Page.Validators[i];
180                                         if (!currentValidator.IsValid)
181                                         {
182                                                 showSummary = true;
183                                                 if (currentValidator.ErrorMessage != null &&
184                                                     currentValidator.ErrorMessage.Length != 0)
185                                                         numOfMsg++;
186                                         }
187                                 }
188         
189                                 if (numOfMsg != 0)
190                                 {
191                                         messages = new string[(int)numOfMsg];
192                                         int cur_msg = 0;
193                                         for (int i = 0; i < base.Page.Validators.Count; i++)
194                                         {
195                                                 IValidator currentValidator = base.Page.Validators[i];
196                                                 if (!currentValidator.IsValid &&
197                                                      currentValidator.ErrorMessage != null &&
198                                                      currentValidator.ErrorMessage.Length != 0) 
199                                                         messages[cur_msg++] = String.Copy(currentValidator.ErrorMessage);
200                                         }
201                                 }
202         
203                                 toDisplay = ShowSummary ? showSummary : false;
204                                 if (!toDisplay && uplevelRender)
205                                         base.Style["display"] = "none";
206                         }
207                         if (base.Page != null)
208                         {
209                                 base.Page.VerifyRenderingInServerForm(this);
210                         }
211                         bool tagRequired = !uplevelRender ? toDisplay : true;
212                         if (tagRequired)
213                         {
214                                 base.RenderBeginTag(writer);
215                         }
216                         if (toDisplay)
217                         {
218                                 string str1, str2, str3, str4, str5;
219                                
220         
221                                 switch (DisplayMode)
222                                 {
223                                 case ValidationSummaryDisplayMode.List:
224                                         str1 = "<br>";
225                                         str2 = "";
226                                         str3 = "";
227                                         str4 = "<br>";
228                                         str5 = "";
229                                         break;
230                                         
231                                 case ValidationSummaryDisplayMode.SingleParagraph:
232                                         str1 = " ";
233                                         str2 = "";
234                                         str3 = "";
235                                         str4 = " ";
236                                         str5 = "<br>";
237                                         break;
238                                         
239                                 default:
240                                         str1 = "";
241                                         str2 = "<ul>";
242                                         str3 = "<li>";
243                                         str4 = "</li>";
244                                         str5 = "</ul>";
245                                         break;
246                                 }
247                                 if (HeaderText.Length > 0)
248                                 {
249                                         writer.Write(HeaderText);
250                                         writer.Write(str1);
251                                 }
252                                 writer.Write(str2);
253                                 if (messages != null)
254                                 {
255                                         for (int i = 0; i < (int)messages.Length; i++)
256                                         {
257                                                 writer.Write(str3);
258                                                 writer.Write(messages[i]);
259                                                 writer.Write(str4);
260                                         }
261                                 }
262                                 writer.Write(str5);
263                         }
264                         if (tagRequired)
265                         {
266                                 base.RenderEndTag(writer);
267                         }
268                 }
269         }
270 }\r