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;
30 using System.Collections.Specialized;
31 using System.ComponentModel;
32 using System.Globalization;
33 using System.Security.Permissions;
35 namespace System.Web.UI.WebControls {
37 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
41 [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
42 [DefaultEvent ("CheckedChanged")]
43 [DefaultProperty ("Text")]
45 [ControlValueProperty ("Checked", null)]
46 [SupportsEventValidation]
48 public class CheckBox : WebControl, IPostBackDataHandler
54 AttributeCollection common_attrs;
57 AttributeCollection inputAttributes;
58 StateBag inputAttributesState;
59 AttributeCollection labelAttributes;
60 StateBag labelAttributesState;
63 public CheckBox () : base (HtmlTextWriterTag.Input)
65 render_type = "checkbox";
68 internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
70 this.render_type = render_type;
73 [DefaultValue (false)]
77 [WebSysDescription ("")]
78 [WebCategory ("Behavior")]
79 public virtual bool AutoPostBack
82 return (ViewState.GetBool ("AutoPostBack",
86 ViewState["AutoPostBack"] = value;
91 [DefaultValue (false)]
94 [WebSysDescription ("")]
95 [WebCategory ("Behavior")]
96 public virtual bool CausesValidation
98 get { return ViewState.GetBool ("CausesValidation", false); }
99 set { ViewState ["CausesValidation"] = value; }
104 [DefaultValue (false)]
106 [Bindable (true, BindingDirection.TwoWay)]
111 [WebSysDescription ("")]
112 [WebCategory ("Behavior")]
113 public virtual bool Checked
116 return (ViewState.GetBool ("Checked", false));
119 ViewState["Checked"] = value;
125 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
126 public AttributeCollection InputAttributes
129 if (inputAttributes == null) {
130 if (inputAttributesState == null) {
131 inputAttributesState = new StateBag (true);
132 if (IsTrackingViewState)
133 inputAttributesState.TrackViewState();
135 inputAttributes = new AttributeCollection (inputAttributesState);
137 return inputAttributes;
142 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
143 public AttributeCollection LabelAttributes
146 if (labelAttributes == null) {
147 if (labelAttributesState == null) {
148 labelAttributesState = new StateBag (true);
149 if (IsTrackingViewState)
150 labelAttributesState.TrackViewState();
152 labelAttributes = new AttributeCollection (labelAttributesState);
154 return labelAttributes;
164 [WebSysDescription ("")]
165 [WebCategory ("Appearance")]
166 public virtual string Text
169 return (ViewState.GetString ("Text",
173 ViewState["Text"] = value;
177 [DefaultValue (TextAlign.Right)]
181 [WebSysDescription ("")]
182 [WebCategory ("Appearance")]
183 public virtual TextAlign TextAlign
185 get { return (TextAlign) ViewState.GetInt ("TextAlign", (int)TextAlign.Right); }
187 if (value != TextAlign.Left &&
188 value != TextAlign.Right) {
189 throw new ArgumentOutOfRangeException ("value");
192 ViewState["TextAlign"] = value;
199 [WebSysDescription ("")]
200 [WebCategoryAttribute ("Behavior")]
201 public virtual string ValidationGroup
203 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
204 set { ViewState["ValidationGroup"] = value; }
208 private static readonly object EventCheckedChanged = new object ();
209 [WebSysDescription ("")]
210 [WebCategory ("Action")]
211 public event EventHandler CheckedChanged
214 Events.AddHandler (EventCheckedChanged, value);
217 Events.RemoveHandler (EventCheckedChanged, value);
221 protected virtual void OnCheckedChanged (EventArgs e)
223 EventHandler handler = (EventHandler)Events[EventCheckedChanged];
225 if (handler != null) {
230 internal virtual string NameAttribute
233 return (this.UniqueID);
238 protected override void LoadViewState (object savedState)
240 if (savedState == null) {
241 base.LoadViewState (null);
245 Triplet saved = (Triplet) savedState;
246 base.LoadViewState (saved.First);
248 if (saved.Second != null) {
249 if (inputAttributesState == null) {
250 inputAttributesState = new StateBag(true);
251 inputAttributesState.TrackViewState ();
253 inputAttributesState.LoadViewState (saved.Second);
256 if (saved.Third != null) {
257 if (labelAttributesState == null) {
258 labelAttributesState = new StateBag(true);
259 labelAttributesState.TrackViewState ();
261 labelAttributesState.LoadViewState (saved.Third);
265 protected override object SaveViewState ()
267 object baseView = base.SaveViewState ();
268 object inputAttrView = null;
269 object labelAttrView = null;
271 if (inputAttributesState != null)
272 inputAttrView = inputAttributesState.SaveViewState ();
274 if (labelAttributesState != null)
275 labelAttrView = labelAttributesState.SaveViewState ();
277 if (baseView == null && inputAttrView == null && labelAttrView == null)
280 return new Triplet (baseView, inputAttrView, labelAttrView);
283 protected override void TrackViewState ()
285 base.TrackViewState();
286 if (inputAttributesState != null)
287 inputAttributesState.TrackViewState ();
288 if (labelAttributesState != null)
289 labelAttributesState.TrackViewState ();
298 override void OnPreRender (EventArgs e)
300 base.OnPreRender (e);
303 Page.RegisterRequiresPostBack (this);
307 static bool IsInputOrCommonAttr (string attname)
309 attname = attname.ToUpper (CultureInfo.InvariantCulture);
343 void AddAttributesForSpan (HtmlTextWriter writer)
345 ICollection k = Attributes.Keys;
346 string [] keys = new string [k.Count];
348 foreach (string key in keys) {
349 if (!IsInputOrCommonAttr (key))
351 if (common_attrs == null)
352 common_attrs = new AttributeCollection (new StateBag ());
353 common_attrs [key] = Attributes [key];
354 Attributes.Remove (key);
356 Attributes.AddAttributes (writer);
363 override void Render (HtmlTextWriter w)
366 Page.VerifyRenderingInServerForm (this);
368 bool need_span = ControlStyleCreated && !ControlStyle.IsEmpty;
370 ControlStyle.AddAttributesToRender (w, this);
373 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
379 w.AddAttribute ("title", tt);
383 if (Attributes.Count > 0){
384 AddAttributesForSpan (w);
385 Attributes.AddAttributes (w);
390 w.RenderBeginTag (HtmlTextWriterTag.Span);
392 TextAlign align = TextAlign;
393 if (align == TextAlign.Right) {
394 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
395 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
396 w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
397 InternalAddAttributesToRender (w);
399 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
402 w.AddAttribute (HtmlTextWriterAttribute.Onclick,
403 Page.ClientScript.GetPostBackEventReference (this, String.Empty));
404 w.AddAttribute ("language", "javascript");
407 if (AccessKey.Length > 0)
408 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
411 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
412 TabIndex.ToString (CultureInfo.InvariantCulture));
414 if (common_attrs != null)
415 common_attrs.AddAttributes (w);
416 w.RenderBeginTag (HtmlTextWriterTag.Input);
421 if (labelAttributes != null)
422 labelAttributes.AddAttributes (w);
424 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
425 w.RenderBeginTag (HtmlTextWriterTag.Label);
433 if (labelAttributes != null)
434 labelAttributes.AddAttributes (w);
436 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
437 w.RenderBeginTag (HtmlTextWriterTag.Label);
442 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
443 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
444 w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
445 InternalAddAttributesToRender (w);
447 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
450 w.AddAttribute (HtmlTextWriterAttribute.Onclick,
451 Page.ClientScript.GetPostBackEventReference (this, String.Empty));
452 w.AddAttribute ("language", "javascript");
455 if (AccessKey.Length > 0)
456 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
459 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
460 TabIndex.ToString (NumberFormatInfo.InvariantInfo));
462 if (common_attrs != null)
463 common_attrs.AddAttributes (w);
464 w.RenderBeginTag (HtmlTextWriterTag.Input);
475 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
477 string postedValue = postCollection[postDataKey];
478 bool postedBool = ((postedValue != null) &&
479 (postedValue.Length > 0));
481 if (Checked != postedBool) {
482 Checked = postedBool;
492 void RaisePostDataChangedEvent ()
495 if (CausesValidation)
496 Page.Validate (ValidationGroup);
498 OnCheckedChanged (EventArgs.Empty);
501 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
503 return LoadPostData (postDataKey, postCollection);
506 void IPostBackDataHandler.RaisePostDataChangedEvent ()
508 RaisePostDataChangedEvent ();
512 protected override void AddAttributesToRender (HtmlTextWriter writer)
514 base.AddAttributesToRender (writer);
518 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
521 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");