2 // System.Web.UI.WebControls.CheckBoxList.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections.Specialized;
\r
35 using System.ComponentModel;
\r
36 using System.Globalization;
\r
38 using System.Web.UI;
\r
40 namespace System.Web.UI.WebControls
\r
42 public class CheckBoxList: ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
\r
44 CheckBox checkBoxRepeater;
\r
45 bool isChangeNotified;
\r
47 public CheckBoxList()
\r
49 checkBoxRepeater = new CheckBox();
\r
50 checkBoxRepeater.ID = "0";
\r
51 checkBoxRepeater.EnableViewState = false;
\r
52 Controls.Add (checkBoxRepeater);
\r
53 isChangeNotified = false;
\r
56 [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
57 [WebSysDescription ("The border left within a CheckBox.")]
\r
58 public virtual int CellPadding
\r
62 return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellPadding : -1);
\r
66 ((TableStyle)ControlStyle).CellPadding = value;
\r
70 [DefaultValue (-1), Bindable (true), WebCategory ("Layout")]
71 [WebSysDescription ("The border left between CheckBoxes.")]
\r
72 public virtual int CellSpacing
\r
76 return (ControlStyleCreated ? ((TableStyle)ControlStyle).CellSpacing : -1);
\r
80 ((TableStyle)ControlStyle).CellSpacing = value;
\r
84 [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
85 [WebSysDescription ("The number of columns that should be used to display the CheckBoxes.")]
\r
86 public virtual int RepeatColumns
\r
90 object o = ViewState["RepeatColumns"];
\r
98 throw new ArgumentOutOfRangeException();
\r
99 ViewState["RepeatColumns"] = value;
\r
103 [DefaultValue (typeof (RepeatDirection), "Vertical"), Bindable (true), WebCategory ("Layout")]
104 [WebSysDescription ("The direction that is followed when doing the layout.")]
\r
105 public virtual RepeatDirection RepeatDirection
\r
109 object o = ViewState["RepeatDirection"];
\r
111 return (RepeatDirection)o;
\r
112 return RepeatDirection.Vertical;
\r
116 if(!System.Enum.IsDefined(typeof(RepeatDirection),value))
\r
117 throw new ArgumentException();
\r
118 ViewState["RepeatDirection"] = value;
\r
122 [DefaultValue (typeof (RepeatLayout), "Table"), Bindable (true), WebCategory ("Layout")]
123 [WebSysDescription ("The method used to create the layout.")]
\r
124 public virtual RepeatLayout RepeatLayout
\r
128 object o = ViewState["RepeatLayout"];
\r
130 return (RepeatLayout)o;
\r
131 return RepeatLayout.Table;
\r
135 if(!System.Enum.IsDefined(typeof(RepeatLayout), value))
\r
136 throw new ArgumentException();
\r
137 ViewState["RepeatLayout"] = value;
\r
141 [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
142 [WebSysDescription ("The alignment of the CheckBox text.")]
\r
143 public virtual TextAlign TextAlign
\r
147 object o = ViewState["TextAlign"];
\r
149 return (TextAlign)o;
\r
150 return TextAlign.Right;
\r
154 if(!Enum.IsDefined(typeof(TextAlign), value))
\r
155 throw new ArgumentException();
\r
156 ViewState["TextAlign"] = value;
\r
160 protected override Style CreateControlStyle()
\r
162 return new TableStyle(ViewState);
\r
165 protected override Control FindControl(string id, int pathOffset)
\r
170 protected override void OnPreRender(EventArgs e)
\r
172 checkBoxRepeater.AutoPostBack = AutoPostBack;
\r
175 for(int i=0; i < Items.Count; i++)
\r
177 if(Items[i].Selected)
\r
179 checkBoxRepeater.ID = i.ToString(NumberFormatInfo.InvariantInfo);
\r
180 Page.RegisterRequiresPostBack(checkBoxRepeater);
\r
186 protected override void Render(HtmlTextWriter writer)
\r
188 RepeatInfo ri = new RepeatInfo();
\r
189 checkBoxRepeater.TabIndex = TabIndex;
\r
190 bool dirtyFlag = false;
\r
191 short tTabIndex = TabIndex;
\r
192 Style s = (ControlStyleCreated ? ControlStyle : null);
\r
195 if(!ViewState.IsItemDirty("TabIndex"))
\r
199 ri.RepeatColumns = RepeatColumns;
\r
200 ri.RepeatLayout = RepeatLayout;
\r
201 ri.RepeatDirection = RepeatDirection;
\r
202 ri.RenderRepeater(writer, this, s, this);
\r
205 TabIndex = tTabIndex;
\r
209 ViewState.SetItemDirty("TabIndex", false);
\r
213 bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
218 int index = Int32.Parse(postDataKey.Substring(UniqueID.Length + 1));
\r
219 if(index >= 0 && index < Items.Count)
221 string v = postCollection [postDataKey];
222 bool exists = (v != null);
223 if(Items[index].Selected != exists)
\r
225 Items[index].Selected = exists;
\r
226 if(!isChangeNotified)
\r
228 isChangeNotified = true;
\r
236 void IPostBackDataHandler.RaisePostDataChangedEvent()
\r
238 OnSelectedIndexChanged(EventArgs.Empty);
\r
241 bool IRepeatInfoUser.HasFooter
\r
249 bool IRepeatInfoUser.HasHeader
\r
257 bool IRepeatInfoUser.HasSeparators
\r
265 int IRepeatInfoUser.RepeatedItemCount
\r
269 return Items.Count;
\r
273 Style IRepeatInfoUser.GetItemStyle(ListItemType itemType, int repeatIndex)
\r
278 void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
\r
280 checkBoxRepeater.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);
\r
281 checkBoxRepeater.Text = Items[repeatIndex].Text;
\r
282 checkBoxRepeater.TextAlign = TextAlign;
\r
283 checkBoxRepeater.Checked = Items[repeatIndex].Selected;
\r
284 checkBoxRepeater.Enabled = Enabled;
\r
285 checkBoxRepeater.RenderControl(writer);
\r