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;
33 using System.Web.Util;
35 namespace System.Web.UI.WebControls {
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 public class CheckBoxList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
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 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 (Helpers.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, Helpers.InvariantCulture);
235 string val = postCollection [postDataKey];
236 bool ischecked = val == "on";
237 ListItem item = Items [checkbox];
242 if (ischecked && !item.Selected) {
243 item.Selected = true;
253 void RaisePostDataChangedEvent ()
256 if (CausesValidation)
257 Page.Validate (ValidationGroup);
259 OnSelectedIndexChanged (EventArgs.Empty);
262 bool IPostBackDataHandler.LoadPostData (string postDataKey,
263 NameValueCollection postCollection)
265 return LoadPostData (postDataKey, postCollection);
268 void IPostBackDataHandler.RaisePostDataChangedEvent ()
270 RaisePostDataChangedEvent ();
283 bool IRepeatInfoUser.HasFooter {
284 get { return HasFooter; }
297 bool IRepeatInfoUser.HasHeader {
298 get { return HasHeader; }
312 bool IRepeatInfoUser.HasSeparators {
313 get { return HasSeparators; }
319 int RepeatedItemCount
326 int IRepeatInfoUser.RepeatedItemCount {
327 get { return RepeatedItemCount; }
333 Style GetItemStyle (ListItemType itemType,
339 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType,
342 return GetItemStyle (itemType, repeatIndex);
348 void RenderItem (ListItemType itemType,
350 RepeatInfo repeatInfo,
351 HtmlTextWriter writer)
353 ListItem item = Items [repeatIndex];
355 check_box.ID = repeatIndex.ToString (Helpers.InvariantCulture);
356 check_box.Text = item.Text;
357 check_box.AutoPostBack = AutoPostBack;
358 check_box.Checked = item.Selected;
359 check_box.TextAlign = TextAlign;
361 check_box.Enabled = false;
363 check_box.Enabled = item.Enabled;
365 check_box.ValidationGroup = ValidationGroup;
366 check_box.CausesValidation = CausesValidation;
367 if (check_box.HasAttributes)
368 check_box.Attributes.Clear ();
369 if (item.HasAttributes)
370 check_box.Attributes.CopyFrom (item.Attributes);
372 check_box.RenderControl (writer);
375 void IRepeatInfoUser.RenderItem (ListItemType itemType,
376 int repeatIndex, RepeatInfo repeatInfo,
377 HtmlTextWriter writer)
379 RenderItem (itemType, repeatIndex, repeatInfo, writer);
382 protected internal override void VerifyMultiSelect()
384 //by default the ListControl will throw an exception in this method,
385 //therefor we should override the method if the class is supporting
386 //MultiSelect option.