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); }
99 outOfRange = value < RepeatLayout.Table || value > RepeatLayout.OrderedList;
101 outOfRange = value < RepeatLayout.Table || value > RepeatLayout.Flow;
104 throw new ArgumentOutOfRangeException ("value");
105 ViewState ["RepeatLayout"] = value;
109 [DefaultValue(TextAlign.Right)]
110 [WebSysDescription ("")]
111 [WebCategory ("Appearance")]
112 public virtual TextAlign TextAlign {
113 get { return (TextAlign) ViewState.GetInt ("TextAlign", (int) TextAlign.Right); }
115 if (value < TextAlign.Left || value > TextAlign.Right)
116 throw new ArgumentOutOfRangeException ("value");
117 ViewState ["TextAlign"] = value;
121 TableStyle TableStyle {
122 get { return (TableStyle) ControlStyle; }
125 protected override Style CreateControlStyle ()
127 return new TableStyle (ViewState);
130 protected override Control FindControl (string id, int pathOffset)
132 // Always, or in just all my tests?
136 protected internal override void OnPreRender (EventArgs e)
138 base.OnPreRender (e);
140 // Register all of the checked controls so we can
141 // find out when they are unchecked.
143 for (int i = 0; i < Items.Count; i++) {
144 if (Items [i].Selected) {
145 check_box.ID = i.ToString (Helpers.InvariantCulture);
147 page.RegisterRequiresPostBack (check_box);
152 protected internal override void Render (HtmlTextWriter writer)
154 if (Items.Count == 0)
157 RepeatInfo ri = new RepeatInfo ();
158 ri.RepeatColumns = RepeatColumns;
159 ri.RepeatDirection = RepeatDirection;
160 ri.RepeatLayout = RepeatLayout;
164 check_box.TabIndex = TabIndex;
169 string ak = AccessKey;
170 check_box.AccessKey = ak;
171 this.AccessKey = null;
173 ri.RenderRepeater (writer, this, TableStyle, this);
180 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
189 string id = postDataKey.Substring (ClientID.Length + 1);
190 if (Char.IsDigit (id [0]))
191 checkbox = Int32.Parse (id, Helpers.InvariantCulture);
199 string val = postCollection [postDataKey];
200 bool ischecked = val == "on";
202 if(!RenderingCompatibilityLessThan40 && val != null){
203 ischecked = val == Items[checkbox].Value;
206 ListItem item = Items [checkbox];
208 if (ischecked && !item.Selected) {
209 item.Selected = true;
211 } else if (!ischecked && item.Selected) {
212 item.Selected = false;
220 protected virtual void RaisePostDataChangedEvent ()
222 if (CausesValidation) {
225 page.Validate (ValidationGroup);
228 OnSelectedIndexChanged (EventArgs.Empty);
231 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
233 return LoadPostData (postDataKey, postCollection);
236 void IPostBackDataHandler.RaisePostDataChangedEvent ()
238 RaisePostDataChangedEvent ();
241 protected virtual bool HasFooter {
242 get { return false; }
245 bool IRepeatInfoUser.HasFooter {
246 get { return HasFooter; }
249 protected virtual bool HasHeader {
250 get { return false; }
253 bool IRepeatInfoUser.HasHeader {
254 get { return HasHeader; }
257 protected virtual bool HasSeparators {
258 get { return false; }
261 bool IRepeatInfoUser.HasSeparators {
262 get { return HasSeparators; }
265 protected virtual int RepeatedItemCount {
266 get { return Items.Count; }
269 int IRepeatInfoUser.RepeatedItemCount {
270 get { return RepeatedItemCount; }
273 protected virtual Style GetItemStyle (ListItemType itemType, int repeatIndex)
278 Style IRepeatInfoUser.GetItemStyle (ListItemType itemType, int repeatIndex)
280 return GetItemStyle (itemType, repeatIndex);
283 protected virtual void RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
285 ListItem item = Items [repeatIndex];
287 string cssClass = check_box.CssClass;
288 if (!String.IsNullOrEmpty (cssClass))
289 check_box.CssClass = String.Empty;
290 check_box.ID = repeatIndex.ToString (Helpers.InvariantCulture);
291 check_box.Text = item.Text;
292 check_box.AutoPostBack = AutoPostBack;
293 check_box.Checked = item.Selected;
294 check_box.TextAlign = TextAlign;
296 check_box.Enabled = false;
298 check_box.Enabled = item.Enabled;
300 check_box.ValidationGroup = ValidationGroup;
301 check_box.CausesValidation = CausesValidation;
302 if (check_box.HasAttributes)
303 check_box.Attributes.Clear ();
304 if (item.HasAttributes)
305 check_box.Attributes.CopyFrom (item.Attributes);
307 if (!RenderingCompatibilityLessThan40) {
308 var attrs = check_box.InputAttributes;
311 attrs.Add ("value", item.Value);
314 check_box.RenderControl (writer);
317 void IRepeatInfoUser.RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
319 RenderItem (itemType, repeatIndex, repeatInfo, writer);
322 internal override bool MultiSelectOk ()