c3be3f946419295bbf6e7e522a7b663501c422ff
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / CheckBoxList.cs
1 //
2 // System.Web.UI.WebControls.CheckBoxList.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 //
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;\r
34 using System.Collections.Specialized;\r
35 using System.ComponentModel;\r
36 using System.Globalization;\r
37 using System.Web;\r
38 using System.Web.UI;\r
39 \r
40 namespace System.Web.UI.WebControls\r
41 {\r
42         public class CheckBoxList: ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler\r
43         {\r
44                 CheckBox checkBoxRepeater;\r
45                 bool     isChangeNotified;\r
46 \r
47                 public CheckBoxList()\r
48                 {\r
49                         checkBoxRepeater = new CheckBox();\r
50                         checkBoxRepeater.ID = "0";\r
51                         checkBoxRepeater.EnableViewState = false;\r
52                         Controls.Add (checkBoxRepeater);\r
53                         isChangeNotified = false;\r
54                 }\r
55
56                 [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
57                 [WebSysDescription ("The border left within a CheckBox.")]\r
58                 public virtual int CellPadding\r
59                 {\r
60                         get\r
61                         {\r
62                                 return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellPadding : -1);\r
63                         }\r
64                         set\r
65                         {\r
66                                 ((TableStyle)ControlStyle).CellPadding = value;\r
67                         }\r
68                 }\r
69
70                 [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
71                 [WebSysDescription ("The border left between CheckBoxes.")]\r
72                 public virtual int CellSpacing\r
73                 {\r
74                         get\r
75                         {\r
76                                 return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellSpacing : -1);\r
77                         }\r
78                         set\r
79                         {\r
80                                 ((TableStyle)ControlStyle).CellSpacing = value;\r
81                         }\r
82                 }\r
83
84                 [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
85                 [WebSysDescription ("The number of columns that should be used to display the CheckBoxes.")]\r
86                 public virtual int RepeatColumns\r
87                 {\r
88                         get\r
89                         {\r
90                                 object o = ViewState["RepeatColumns"];\r
91                                 if(o!=null)\r
92                                         return (int)o;\r
93                                 return 0;\r
94                         }\r
95                         set\r
96                         {\r
97                                 if(value < 0)\r
98                                         throw new ArgumentOutOfRangeException();\r
99                                 ViewState["RepeatColumns"] = value;\r
100                         }\r
101                 }\r
102
103                 [DefaultValue (typeof (RepeatDirection), "Vertical"), Bindable (true), WebCategory ("Layout")]
104                 [WebSysDescription ("The direction that is followed when doing the layout.")]\r
105                 public virtual RepeatDirection RepeatDirection\r
106                 {\r
107                         get\r
108                         {\r
109                                 object o = ViewState["RepeatDirection"];\r
110                                 if(o!=null)\r
111                                         return (RepeatDirection)o;\r
112                                 return RepeatDirection.Vertical;\r
113                         }\r
114                         set\r
115                         {\r
116                                 if(!System.Enum.IsDefined(typeof(RepeatDirection),value))\r
117                                         throw new ArgumentException();\r
118                                 ViewState["RepeatDirection"] = value;\r
119                         }\r
120                 }\r
121
122                 [DefaultValue (typeof (RepeatLayout), "Table"), Bindable (true), WebCategory ("Layout")]
123                 [WebSysDescription ("The method used to create the layout.")]\r
124                 public virtual RepeatLayout RepeatLayout\r
125                 {\r
126                         get\r
127                         {\r
128                                 object o = ViewState["RepeatLayout"];\r
129                                 if(o!=null)\r
130                                         return (RepeatLayout)o;\r
131                                 return RepeatLayout.Table;\r
132                         }\r
133                         set\r
134                         {\r
135                                 if(!System.Enum.IsDefined(typeof(RepeatLayout), value))\r
136                                         throw new ArgumentException();\r
137                                 ViewState["RepeatLayout"] = value;\r
138                         }\r
139                 }\r
140
141                 [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
142                 [WebSysDescription ("The alignment of the CheckBox text.")]\r
143                 public virtual TextAlign TextAlign\r
144                 {\r
145                         get\r
146                         {\r
147                                 object o = ViewState["TextAlign"];\r
148                                 if(o!=null)\r
149                                         return (TextAlign)o;\r
150                                 return TextAlign.Right;\r
151                         }\r
152                         set\r
153                         {\r
154                                 if(!Enum.IsDefined(typeof(TextAlign), value))\r
155                                         throw new ArgumentException();\r
156                                 ViewState["TextAlign"] = value;\r
157                         }\r
158                 }\r
159 \r
160                 protected override Style CreateControlStyle()\r
161                 {\r
162                         return new TableStyle(ViewState);\r
163                 }\r
164 \r
165                 protected override Control FindControl(string id, int pathOffset)\r
166                 {\r
167                         return this;\r
168                 }\r
169 \r
170                 protected override void OnPreRender(EventArgs e)\r
171                 {\r
172                         checkBoxRepeater.AutoPostBack = AutoPostBack;\r
173                         if(Page!=null)\r
174                         {\r
175                                 for(int i=0; i < Items.Count; i++)\r
176                                 {\r
177                                         if(Items[i].Selected)\r
178                                         {\r
179                                                 checkBoxRepeater.ID = i.ToString(NumberFormatInfo.InvariantInfo);\r
180                                                 Page.RegisterRequiresPostBack(checkBoxRepeater);\r
181                                         }\r
182                                 }\r
183                         }\r
184                 }\r
185 \r
186                 protected override void Render(HtmlTextWriter writer)\r
187                 {\r
188                         RepeatInfo ri = new RepeatInfo();\r
189                         checkBoxRepeater.TabIndex = TabIndex;\r
190                         bool dirtyFlag = false;\r
191                         short  tTabIndex = TabIndex;\r
192                         Style s = (ControlStyleCreated ? ControlStyle : null);\r
193                         if(TabIndex != 0)\r
194                         {\r
195                                 if(!ViewState.IsItemDirty("TabIndex"))\r
196                                         dirtyFlag = true;\r
197                                 TabIndex = 0;\r
198                         }\r
199                         ri.RepeatColumns = RepeatColumns;\r
200                         ri.RepeatLayout  = RepeatLayout;\r
201                         ri.RepeatDirection = RepeatDirection;\r
202                         ri.RenderRepeater(writer, this, s, this);\r
203                         if(tTabIndex != 0)\r
204                         {\r
205                                 TabIndex = tTabIndex;\r
206                         }\r
207                         if(dirtyFlag)\r
208                         {\r
209                                 ViewState.SetItemDirty("TabIndex", false);\r
210                         }\r
211                 }\r
212
213                 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
214                 {
215                         if (!Enabled)
216                                 return false;
217
218                         int index = Int32.Parse(postDataKey.Substring(UniqueID.Length + 1));\r
219                         if(index >= 0 && index < Items.Count)
220                         {
221                                 string v = postCollection [postDataKey];
222                                 bool exists = (v != null);
223                                 if(Items[index].Selected != exists)\r
224                                 {\r
225                                         Items[index].Selected = exists;\r
226                                         if(!isChangeNotified)\r
227                                         {\r
228                                                 isChangeNotified = true;\r
229                                                 return true;\r
230                                         }\r
231                                 }\r
232                         }\r
233                         return false;\r
234                 }\r
235 \r
236                 void IPostBackDataHandler.RaisePostDataChangedEvent()\r
237                 {\r
238                         OnSelectedIndexChanged(EventArgs.Empty);\r
239                 }\r
240 \r
241                 bool IRepeatInfoUser.HasFooter\r
242                 {\r
243                         get\r
244                         {\r
245                                 return false;\r
246                         }\r
247                 }\r
248 \r
249                 bool IRepeatInfoUser.HasHeader\r
250                 {\r
251                         get\r
252                         {\r
253                                 return false;\r
254                         }\r
255                 }\r
256 \r
257                 bool IRepeatInfoUser.HasSeparators\r
258                 {\r
259                         get\r
260                         {\r
261                                 return false;\r
262                         }\r
263                 }\r
264 \r
265                 int IRepeatInfoUser.RepeatedItemCount\r
266                 {\r
267                         get\r
268                         {\r
269                                 return Items.Count;\r
270                         }\r
271                 }\r
272 \r
273                 Style IRepeatInfoUser.GetItemStyle(ListItemType itemType, int repeatIndex)\r
274                 {\r
275                         return null;\r
276                 }\r
277 \r
278                 void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)\r
279                 {\r
280                         checkBoxRepeater.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);\r
281                         checkBoxRepeater.Text = Items[repeatIndex].Text;\r
282                         checkBoxRepeater.TextAlign = TextAlign;\r
283                         checkBoxRepeater.Checked = Items[repeatIndex].Selected;\r
284                         checkBoxRepeater.Enabled = Enabled;\r
285                         checkBoxRepeater.RenderControl(writer);\r
286                 }\r
287         }\r
288 }\r