2 // System.Web.UI.WebControls.CheckBox.cs
5 // Dick Porter <dick@ximian.com>
7 // Copyright (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.Collections.Specialized;
30 using System.ComponentModel;
31 using System.Globalization;
32 using System.Security.Permissions;
34 namespace System.Web.UI.WebControls {
36 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
40 [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
41 [DefaultEvent ("CheckedChanged")]
42 [DefaultProperty ("Text")]
44 [ControlValueProperty ("Checked", null)]
45 [SupportsEventValidation]
47 public class CheckBox : WebControl, IPostBackDataHandler
55 AttributeCollection inputAttributes;
56 StateBag inputAttributesState;
57 AttributeCollection labelAttributes;
58 StateBag labelAttributesState;
61 public CheckBox () : base (HtmlTextWriterTag.Input)
63 render_type = "checkbox";
66 internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
68 this.render_type = render_type;
71 [DefaultValue (false)]
75 [WebSysDescription ("")]
76 [WebCategory ("Behavior")]
77 public virtual bool AutoPostBack
80 return (ViewState.GetBool ("AutoPostBack",
84 ViewState["AutoPostBack"] = value;
89 [DefaultValue (false)]
92 [WebSysDescription ("")]
93 [WebCategory ("Behavior")]
94 public virtual bool CausesValidation
96 get { return ViewState.GetBool ("CausesValidation", false); }
97 set { ViewState ["CausesValidation"] = value; }
102 [DefaultValue (false)]
104 [Bindable (true, BindingDirection.TwoWay)]
109 [WebSysDescription ("")]
110 [WebCategory ("Behavior")]
111 public virtual bool Checked
114 return (ViewState.GetBool ("Checked", false));
117 ViewState["Checked"] = value;
123 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
124 public AttributeCollection InputAttributes
127 if (inputAttributes == null) {
128 if (inputAttributesState == null) {
129 inputAttributesState = new StateBag (true);
130 if (IsTrackingViewState)
131 inputAttributesState.TrackViewState();
133 inputAttributes = new AttributeCollection (inputAttributesState);
135 return inputAttributes;
140 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
141 public AttributeCollection LabelAttributes
144 if (labelAttributes == null) {
145 if (labelAttributesState == null) {
146 labelAttributesState = new StateBag (true);
147 if (IsTrackingViewState)
148 labelAttributesState.TrackViewState();
150 labelAttributes = new AttributeCollection (labelAttributesState);
152 return labelAttributes;
162 [WebSysDescription ("")]
163 [WebCategory ("Appearance")]
164 public virtual string Text
167 return (ViewState.GetString ("Text",
171 ViewState["Text"] = value;
175 [DefaultValue (TextAlign.Right)]
179 [WebSysDescription ("")]
180 [WebCategory ("Appearance")]
181 public virtual TextAlign TextAlign
183 get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
185 if (value != TextAlign.Left &&
186 value != TextAlign.Right) {
187 throw new ArgumentOutOfRangeException ("value");
190 ViewState["TextAlign"] = value;
197 [WebSysDescription ("")]
198 [WebCategoryAttribute ("Behavior")]
199 public string ValidationGroup
201 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
202 set { ViewState["ValidationGroup"] = value; }
206 private static readonly object EventCheckedChanged = new object ();
207 [WebSysDescription ("")]
208 [WebCategory ("Action")]
209 public event EventHandler CheckedChanged
212 Events.AddHandler (EventCheckedChanged, value);
215 Events.RemoveHandler (EventCheckedChanged, value);
219 protected virtual void OnCheckedChanged (EventArgs e)
221 EventHandler handler = (EventHandler)Events[EventCheckedChanged];
223 if (handler != null) {
228 internal virtual string NameAttribute
231 return (this.UniqueID);
236 protected override void LoadViewState (object savedState)
238 if (savedState == null) {
239 base.LoadViewState (null);
243 Triplet saved = (Triplet) savedState;
244 base.LoadViewState (saved.First);
246 if (saved.Second != null) {
247 if (inputAttributesState == null) {
248 inputAttributesState = new StateBag(true);
249 inputAttributesState.TrackViewState ();
251 inputAttributesState.LoadViewState (saved.Second);
254 if (saved.Third != null) {
255 if (labelAttributesState == null) {
256 labelAttributesState = new StateBag(true);
257 labelAttributesState.TrackViewState ();
259 labelAttributesState.LoadViewState (saved.Third);
263 protected override object SaveViewState ()
265 object baseView = base.SaveViewState ();
266 object inputAttrView = null;
267 object labelAttrView = null;
269 if (inputAttributesState != null)
270 inputAttrView = inputAttributesState.SaveViewState ();
272 if (labelAttributesState != null)
273 labelAttrView = labelAttributesState.SaveViewState ();
275 if (baseView == null && inputAttrView == null && labelAttrView == null)
278 return new Triplet (baseView, inputAttrView, labelAttrView);
281 protected override void TrackViewState ()
283 base.TrackViewState();
284 if (inputAttributesState != null)
285 inputAttributesState.TrackViewState ();
286 if (labelAttributesState != null)
287 labelAttributesState.TrackViewState ();
296 override void OnPreRender (EventArgs e)
298 base.OnPreRender (e);
301 Page.RegisterRequiresPostBack (this);
310 override void Render (HtmlTextWriter w)
313 Page.VerifyRenderingInServerForm (this);
315 bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
317 ControlStyle.AddAttributesToRender (w, this);
320 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
326 w.AddAttribute ("title", tt);
330 if (Attributes.Count > 0){
331 Attributes.AddAttributes (w);
336 w.RenderBeginTag (HtmlTextWriterTag.Span);
338 TextAlign align = TextAlign;
339 if (align == TextAlign.Right) {
340 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
341 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
342 w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
343 InternalAddAttributesToRender (w);
345 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
348 w.AddAttribute (HtmlTextWriterAttribute.Onclick,
349 Page.ClientScript.GetPostBackEventReference (this, String.Empty));
350 w.AddAttribute ("language", "javascript");
353 if (AccessKey.Length > 0)
354 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
357 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
358 TabIndex.ToString (CultureInfo.InvariantCulture));
360 w.RenderBeginTag (HtmlTextWriterTag.Input);
365 if (labelAttributes != null)
366 labelAttributes.AddAttributes (w);
368 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
369 w.RenderBeginTag (HtmlTextWriterTag.Label);
377 if (labelAttributes != null)
378 labelAttributes.AddAttributes (w);
380 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
381 w.RenderBeginTag (HtmlTextWriterTag.Label);
386 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
387 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
388 w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
389 InternalAddAttributesToRender (w);
391 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
394 w.AddAttribute (HtmlTextWriterAttribute.Onclick,
395 Page.ClientScript.GetPostBackEventReference (this, String.Empty));
396 w.AddAttribute ("language", "javascript");
399 if (AccessKey.Length > 0)
400 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
403 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
404 TabIndex.ToString (NumberFormatInfo.InvariantInfo));
406 w.RenderBeginTag (HtmlTextWriterTag.Input);
417 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
419 string postedValue = postCollection[postDataKey];
420 bool postedBool = ((postedValue != null) &&
421 (postedValue.Length > 0));
423 if (Checked != postedBool) {
424 Checked = postedBool;
434 void RaisePostDataChangedEvent ()
437 if (CausesValidation)
438 Page.Validate (ValidationGroup);
440 OnCheckedChanged (EventArgs.Empty);
443 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
445 return LoadPostData (postDataKey, postCollection);
448 void IPostBackDataHandler.RaisePostDataChangedEvent ()
450 RaisePostDataChangedEvent ();
454 protected override void AddAttributesToRender (HtmlTextWriter writer)
456 base.AddAttributesToRender (writer);
460 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
463 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");