2 // System.Web.UI.WebControls.CheckBoxList.cs
5 // Jackson Harper (jackson@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Globalization;
31 using System.Collections.Specialized;
32 using System.ComponentModel;
35 namespace System.Web.UI.WebControls {
37 public class CheckBoxList : ListControl, IRepeatInfoUser,
38 INamingContainer, IPostBackDataHandler {
40 private CheckBox check_box;
42 public CheckBoxList ()
44 check_box = new CheckBox ();
45 Controls.Add (check_box);
52 [WebSysDescription ("")]
53 [WebCategory ("Layout")]
54 public virtual int CellPadding {
55 get { return TableStyle.CellPadding; }
56 set { TableStyle.CellPadding = value; }
63 [WebSysDescription ("")]
64 [WebCategory ("Layout")]
65 public virtual int CellSpacing {
66 get { return TableStyle.CellSpacing; }
67 set { TableStyle.CellSpacing = value; }
74 [WebSysDescription ("")]
75 [WebCategory ("Layout")]
76 public virtual int RepeatColumns {
77 get { return ViewState.GetInt ("RepeatColumns", 0); }
80 throw new ArgumentOutOfRangeException ("value");
81 ViewState ["RepeatColumns"] = value;
88 [DefaultValue(RepeatDirection.Vertical)]
89 [WebSysDescription ("")]
90 [WebCategory ("Layout")]
91 public virtual RepeatDirection RepeatDirection {
93 return (RepeatDirection) ViewState.GetInt ("RepeatDirection",
94 (int) RepeatDirection.Vertical);
97 if (value < RepeatDirection.Horizontal ||
98 value > RepeatDirection.Vertical)
99 throw new ArgumentOutOfRangeException ("value");
100 ViewState ["RepeatDirection"] = value;
107 [DefaultValue(RepeatLayout.Table)]
108 [WebSysDescription ("")]
109 [WebCategory ("Layout")]
110 public virtual RepeatLayout RepeatLayout {
112 return (RepeatLayout) ViewState.GetInt ("RepeatLayout",
113 (int) RepeatLayout.Table);
116 if (value < RepeatLayout.Table ||
117 value > RepeatLayout.Flow)
118 throw new ArgumentOutOfRangeException ("value");
119 ViewState ["RepeatLayout"] = value;
126 [DefaultValue(TextAlign.Right)]
127 [WebSysDescription ("")]
128 [WebCategory ("Appearance")]
129 public virtual TextAlign TextAlign {
131 return (TextAlign) ViewState.GetInt ("TextAlign",
132 (int) TextAlign.Right);
135 if (value < TextAlign.Left || value > TextAlign.Right)
136 throw new ArgumentOutOfRangeException ("value");
137 ViewState ["TextAlign"] = value;
143 protected virtual bool HasFooter
146 throw new NotImplementedException ();
151 protected virtual bool HasHeader
154 throw new NotImplementedException ();
159 protected virtual bool HasSeparators
162 throw new NotImplementedException ();
167 protected virtual int RepeatedItemCount
170 throw new NotImplementedException ();
175 private TableStyle TableStyle {
176 get { return (TableStyle) ControlStyle; }
179 protected override Style CreateControlStyle ()
181 return new TableStyle (ViewState);
184 protected override Control FindControl (string id, int pathOffset)
186 // Always, or in just all my tests?
192 protected virtual Style GetItemStyle (ListItemType itemType,
195 throw new NotImplementedException ();
204 override void OnPreRender (EventArgs e)
206 base.OnPreRender (e);
208 // Register all of the checked controls so we can
209 // find out when they are unchecked.
210 for (int i = 0; i < Items.Count; i++) {
211 if (Items [i].Selected) {
212 check_box.ID = i.ToString (CultureInfo.InvariantCulture);
213 Page.RegisterRequiresPostBack (check_box);
223 override void Render (HtmlTextWriter writer)
226 if (Items.Count == 0)
229 RepeatInfo ri = new RepeatInfo ();
230 ri.RepeatColumns = RepeatColumns;
231 ri.RepeatDirection = RepeatDirection;
232 ri.RepeatLayout = RepeatLayout;
236 check_box.TabIndex = TabIndex;
241 ri.RenderRepeater (writer, this, TableStyle, this);
249 protected virtual void RenderItem (ListItemType itemType,
251 RepeatInfo repeatInfo,
254 throw new NotImplementedException ();
262 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
267 string id = postDataKey.Substring (ClientID.Length + 1);
268 if (Char.IsDigit (id [0]))
269 checkbox = Int32.Parse (id, CultureInfo.InvariantCulture);
277 string val = postCollection [postDataKey];
278 bool ischecked = val == "on";
279 ListItem item = Items [checkbox];
281 if (item.Selected != ischecked) {
282 item.Selected = ischecked;
292 void RaisePostDataChangedEvent ()
294 OnSelectedIndexChanged (EventArgs.Empty);
297 bool IPostBackDataHandler.LoadPostData (string postDataKey,
298 NameValueCollection postCollection)
300 return LoadPostData (postDataKey, postCollection);
303 void IPostBackDataHandler.RaisePostDataChangedEvent ()
305 RaisePostDataChangedEvent ();
308 bool IRepeatInfoUser.HasFooter {
309 get { return false; }
312 bool IRepeatInfoUser.HasHeader {
313 get { return false; }
316 bool IRepeatInfoUser.HasSeparators {
317 get { return false; }
320 int IRepeatInfoUser.RepeatedItemCount {
321 get { return Items.Count; }
324 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,
330 void IRepeatInfoUser.RenderItem (ListItemType itemType,
331 int repeatIndex, RepeatInfo repeatInfo,
332 HtmlTextWriter writer)
334 ListItem item = Items [repeatIndex];
336 check_box.ID = repeatIndex.ToString (CultureInfo.InvariantCulture);
337 check_box.Text = item.Text;
338 check_box.AutoPostBack = AutoPostBack;
339 check_box.Checked = item.Selected;
340 check_box.RenderControl (writer);