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.
29 using System.Globalization;
30 using System.Collections.Specialized;
31 using System.ComponentModel;
32 using System.Security.Permissions;
34 namespace System.Web.UI.WebControls {
37 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 public class CheckBoxList : ListControl, IRepeatInfoUser,
40 INamingContainer, IPostBackDataHandler {
42 private CheckBox check_box;
44 public CheckBoxList ()
46 check_box = new CheckBox ();
47 Controls.Add (check_box);
54 [WebSysDescription ("")]
55 [WebCategory ("Layout")]
56 public virtual int CellPadding {
57 get { return TableStyle.CellPadding; }
58 set { TableStyle.CellPadding = value; }
65 [WebSysDescription ("")]
66 [WebCategory ("Layout")]
67 public virtual int CellSpacing {
68 get { return TableStyle.CellSpacing; }
69 set { TableStyle.CellSpacing = value; }
76 [WebSysDescription ("")]
77 [WebCategory ("Layout")]
78 public virtual int RepeatColumns {
79 get { return ViewState.GetInt ("RepeatColumns", 0); }
82 throw new ArgumentOutOfRangeException ("value");
83 ViewState ["RepeatColumns"] = value;
90 [DefaultValue(RepeatDirection.Vertical)]
91 [WebSysDescription ("")]
92 [WebCategory ("Layout")]
93 public virtual RepeatDirection RepeatDirection {
95 return (RepeatDirection) ViewState.GetInt ("RepeatDirection",
96 (int) RepeatDirection.Vertical);
99 if (value < RepeatDirection.Horizontal ||
100 value > RepeatDirection.Vertical)
101 throw new ArgumentOutOfRangeException ("value");
102 ViewState ["RepeatDirection"] = value;
109 [DefaultValue(RepeatLayout.Table)]
110 [WebSysDescription ("")]
111 [WebCategory ("Layout")]
112 public virtual RepeatLayout RepeatLayout {
114 return (RepeatLayout) ViewState.GetInt ("RepeatLayout",
115 (int) RepeatLayout.Table);
118 if (value < RepeatLayout.Table ||
119 value > RepeatLayout.Flow)
120 throw new ArgumentOutOfRangeException ("value");
121 ViewState ["RepeatLayout"] = value;
128 [DefaultValue(TextAlign.Right)]
129 [WebSysDescription ("")]
130 [WebCategory ("Appearance")]
131 public virtual TextAlign TextAlign {
133 return (TextAlign) ViewState.GetInt ("TextAlign",
134 (int) TextAlign.Right);
137 if (value < TextAlign.Left || value > TextAlign.Right)
138 throw new ArgumentOutOfRangeException ("value");
139 ViewState ["TextAlign"] = value;
143 private TableStyle TableStyle {
144 get { return (TableStyle) ControlStyle; }
147 protected override Style CreateControlStyle ()
149 return new TableStyle (ViewState);
152 protected override Control FindControl (string id, int pathOffset)
154 // Always, or in just all my tests?
163 override void OnPreRender (EventArgs e)
165 base.OnPreRender (e);
167 // Register all of the checked controls so we can
168 // find out when they are unchecked.
169 for (int i = 0; i < Items.Count; i++) {
170 if (Items [i].Selected) {
171 check_box.ID = i.ToString (CultureInfo.InvariantCulture);
172 Page.RegisterRequiresPostBack (check_box);
182 override void Render (HtmlTextWriter writer)
185 if (Items.Count == 0)
188 RepeatInfo ri = new RepeatInfo ();
189 ri.RepeatColumns = RepeatColumns;
190 ri.RepeatDirection = RepeatDirection;
191 ri.RepeatLayout = RepeatLayout;
195 check_box.TabIndex = TabIndex;
200 string ak = AccessKey;
201 check_box.AccessKey = ak;
202 this.AccessKey = null;
205 ri.RenderRepeater (writer, this, TableStyle, this);
217 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
225 string id = postDataKey.Substring (ClientID.Length + 1);
226 if (Char.IsDigit (id [0]))
227 checkbox = Int32.Parse (id, CultureInfo.InvariantCulture);
235 string val = postCollection [postDataKey];
236 bool ischecked = val == "on";
237 ListItem item = Items [checkbox];
239 if (item.Selected != ischecked) {
240 item.Selected = ischecked;
250 void RaisePostDataChangedEvent ()
253 if (CausesValidation)
254 Page.Validate (ValidationGroup);
256 OnSelectedIndexChanged (EventArgs.Empty);
259 bool IPostBackDataHandler.LoadPostData (string postDataKey,
260 NameValueCollection postCollection)
262 return LoadPostData (postDataKey, postCollection);
265 void IPostBackDataHandler.RaisePostDataChangedEvent ()
267 RaisePostDataChangedEvent ();
280 bool IRepeatInfoUser.HasFooter {
281 get { return HasFooter; }
294 bool IRepeatInfoUser.HasHeader {
295 get { return HasHeader; }
309 bool IRepeatInfoUser.HasSeparators {
310 get { return HasSeparators; }
316 int RepeatedItemCount
323 int IRepeatInfoUser.RepeatedItemCount {
324 get { return RepeatedItemCount; }
330 Style GetItemStyle (ListItemType itemType,
336 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,
339 return GetItemStyle (itemType, repeatIndex);
345 void RenderItem (ListItemType itemType,
347 RepeatInfo repeatInfo,
348 HtmlTextWriter writer)
350 ListItem item = Items [repeatIndex];
352 check_box.ID = repeatIndex.ToString (CultureInfo.InvariantCulture);
353 check_box.Text = item.Text;
354 check_box.AutoPostBack = AutoPostBack;
355 check_box.Checked = item.Selected;
356 check_box.TextAlign = TextAlign;
357 check_box.Enabled = Enabled;
359 check_box.ValidationGroup = ValidationGroup;
360 check_box.CausesValidation = CausesValidation;
361 if (check_box.HasAttributes)
362 check_box.Attributes.Clear ();
363 if (item.HasAttributes)
364 check_box.Attributes.CopyFrom (item.Attributes);
366 check_box.RenderControl (writer);
369 void IRepeatInfoUser.RenderItem (ListItemType itemType,
370 int repeatIndex, RepeatInfo repeatInfo,
371 HtmlTextWriter writer)
373 RenderItem (itemType, repeatIndex, repeatInfo, writer);
376 protected internal override void VerifyMultiSelect()
378 //by default the ListControl will throw an exception in this method,
379 //therefor we should override the method if the class is supporting
380 //MultiSelect option.