2 // System.Web.UI.WebControls.CheckBoxList.cs
5 // Jackson Harper (jackson@ximian.com)
7 // (C) 2005-2010 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);
51 [WebSysDescription ("")]
52 [WebCategory ("Layout")]
53 public virtual int CellPadding {
54 get { return TableStyle.CellPadding; }
55 set { TableStyle.CellPadding = value; }
59 [WebSysDescription ("")]
60 [WebCategory ("Layout")]
61 public virtual int CellSpacing {
62 get { return TableStyle.CellSpacing; }
63 set { TableStyle.CellSpacing = value; }
67 [WebSysDescription ("")]
68 [WebCategory ("Layout")]
69 public virtual int RepeatColumns {
70 get { return ViewState.GetInt ("RepeatColumns", 0); }
73 throw new ArgumentOutOfRangeException ("value");
74 ViewState ["RepeatColumns"] = value;
78 [DefaultValue(RepeatDirection.Vertical)]
79 [WebSysDescription ("")]
80 [WebCategory ("Layout")]
81 public virtual RepeatDirection RepeatDirection {
82 get { return (RepeatDirection) ViewState.GetInt ("RepeatDirection", (int) RepeatDirection.Vertical); }
84 if (value < RepeatDirection.Horizontal ||
85 value > RepeatDirection.Vertical)
86 throw new ArgumentOutOfRangeException ("value");
87 ViewState ["RepeatDirection"] = value;
91 [DefaultValue(RepeatLayout.Table)]
92 [WebSysDescription ("")]
93 [WebCategory ("Layout")]
94 public virtual RepeatLayout RepeatLayout {
95 get { return (RepeatLayout) ViewState.GetInt ("RepeatLayout", (int) RepeatLayout.Table); }
98 outOfRange = value < RepeatLayout.Table || value > RepeatLayout.OrderedList;
100 throw new ArgumentOutOfRangeException ("value");
101 ViewState ["RepeatLayout"] = value;
105 [DefaultValue(TextAlign.Right)]
106 [WebSysDescription ("")]
107 [WebCategory ("Appearance")]
108 public virtual TextAlign TextAlign {
109 get { return (TextAlign) ViewState.GetInt ("TextAlign", (int) TextAlign.Right); }
111 if (value < TextAlign.Left || value > TextAlign.Right)
112 throw new ArgumentOutOfRangeException ("value");
113 ViewState ["TextAlign"] = value;
117 TableStyle TableStyle {
118 get { return (TableStyle) ControlStyle; }
121 protected override Style CreateControlStyle ()
123 return new TableStyle (ViewState);
126 protected override Control FindControl (string id, int pathOffset)
128 // Always, or in just all my tests?
132 protected internal override void OnPreRender (EventArgs e)
134 base.OnPreRender (e);
136 // Register all of the checked controls so we can
137 // find out when they are unchecked.
139 for (int i = 0; i < Items.Count; i++) {
140 if (Items [i].Selected) {
141 check_box.ID = i.ToString (Helpers.InvariantCulture);
143 page.RegisterRequiresPostBack (check_box);
148 protected internal override void Render (HtmlTextWriter writer)
150 if (Items.Count == 0)
153 RepeatInfo ri = new RepeatInfo ();
154 ri.RepeatColumns = RepeatColumns;
155 ri.RepeatDirection = RepeatDirection;
156 ri.RepeatLayout = RepeatLayout;
160 check_box.TabIndex = TabIndex;
165 string ak = AccessKey;
166 check_box.AccessKey = ak;
167 this.AccessKey = null;
169 ri.RenderRepeater (writer, this, TableStyle, this);
176 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
185 string id = postDataKey.Substring (ClientID.Length + 1);
186 if (Char.IsDigit (id [0]))
187 checkbox = Int32.Parse (id, Helpers.InvariantCulture);
195 string val = postCollection [postDataKey];
196 bool ischecked = val == "on";
197 ListItem item = Items [checkbox];
199 if (ischecked && !item.Selected) {
200 item.Selected = true;
202 } else if (!ischecked && item.Selected) {
203 item.Selected = false;
211 protected virtual void RaisePostDataChangedEvent ()
213 if (CausesValidation) {
216 page.Validate (ValidationGroup);
219 OnSelectedIndexChanged (EventArgs.Empty);
222 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
224 return LoadPostData (postDataKey, postCollection);
227 void IPostBackDataHandler.RaisePostDataChangedEvent ()
229 RaisePostDataChangedEvent ();
232 protected virtual bool HasFooter {
233 get { return false; }
236 bool IRepeatInfoUser.HasFooter {
237 get { return HasFooter; }
240 protected virtual bool HasHeader {
241 get { return false; }
244 bool IRepeatInfoUser.HasHeader {
245 get { return HasHeader; }
248 protected virtual bool HasSeparators {
249 get { return false; }
252 bool IRepeatInfoUser.HasSeparators {
253 get { return HasSeparators; }
256 protected virtual int RepeatedItemCount {
257 get { return Items.Count; }
260 int IRepeatInfoUser.RepeatedItemCount {
261 get { return RepeatedItemCount; }
264 protected virtual Style GetItemStyle (ListItemType itemType, int repeatIndex)
269 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
271 return GetItemStyle (itemType, repeatIndex);
274 protected virtual void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
276 ListItem item = Items [repeatIndex];
278 string cssClass = check_box.CssClass;
279 if (!String.IsNullOrEmpty (cssClass))
280 check_box.CssClass = String.Empty;
281 check_box.ID = repeatIndex.ToString (Helpers.InvariantCulture);
282 check_box.Text = item.Text;
283 check_box.AutoPostBack = AutoPostBack;
284 check_box.Checked = item.Selected;
285 check_box.TextAlign = TextAlign;
287 check_box.Enabled = false;
289 check_box.Enabled = item.Enabled;
291 check_box.ValidationGroup = ValidationGroup;
292 check_box.CausesValidation = CausesValidation;
293 if (check_box.HasAttributes)
294 check_box.Attributes.Clear ();
295 if (item.HasAttributes)
296 check_box.Attributes.CopyFrom (item.Attributes);
297 if (!RenderingCompatibilityLessThan40) {
298 var attrs = check_box.InputAttributes;
301 attrs.Add ("value", item.Value);
303 check_box.RenderControl (writer);
306 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
308 RenderItem (itemType, repeatIndex, repeatInfo, writer);
311 internal override bool MultiSelectOk ()