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
184 object o = ViewState["TextAlign"];
187 return (TextAlign.Right);
189 return ((TextAlign)o);
193 if (value != TextAlign.Left &&
194 value != TextAlign.Right) {
195 throw new ArgumentOutOfRangeException ("value");
198 ViewState["TextAlign"] = value;
205 [WebSysDescription ("")]
206 [WebCategoryAttribute ("Behavior")]
207 public string ValidationGroup
209 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
210 set { ViewState["ValidationGroup"] = value; }
214 private static readonly object EventCheckedChanged = new object ();
215 [WebSysDescription ("")]
216 [WebCategory ("Action")]
217 public event EventHandler CheckedChanged
220 Events.AddHandler (EventCheckedChanged, value);
223 Events.RemoveHandler (EventCheckedChanged, value);
227 protected virtual void OnCheckedChanged (EventArgs e)
229 EventHandler handler = (EventHandler)Events[EventCheckedChanged];
231 if (handler != null) {
236 internal virtual string NameAttribute
239 return (this.UniqueID);
244 protected override void LoadViewState (object savedState)
246 if (savedState == null) {
247 base.LoadViewState (null);
251 Triplet saved = (Triplet) savedState;
252 base.LoadViewState (saved.First);
254 if (saved.Second != null) {
255 if (inputAttributesState == null) {
256 inputAttributesState = new StateBag(true);
257 inputAttributesState.TrackViewState ();
259 inputAttributesState.LoadViewState (saved.Second);
262 if (saved.Third != null) {
263 if (labelAttributesState == null) {
264 labelAttributesState = new StateBag(true);
265 labelAttributesState.TrackViewState ();
267 labelAttributesState.LoadViewState (saved.Third);
271 protected override object SaveViewState ()
273 object baseView = base.SaveViewState ();
274 object inputAttrView = null;
275 object labelAttrView = null;
277 if (inputAttributesState != null)
278 inputAttrView = inputAttributesState.SaveViewState ();
280 if (labelAttributesState != null)
281 labelAttrView = labelAttributesState.SaveViewState ();
283 if (baseView == null && inputAttrView == null && labelAttrView == null)
286 return new Triplet (baseView, inputAttrView, labelAttrView);
289 protected override void TrackViewState ()
291 base.TrackViewState();
292 if (inputAttributesState != null)
293 inputAttributesState.TrackViewState ();
294 if (labelAttributesState != null)
295 labelAttributesState.TrackViewState ();
304 override void OnPreRender (EventArgs e)
306 base.OnPreRender (e);
309 Page.RegisterRequiresPostBack (this);
318 override void Render (HtmlTextWriter w)
321 Page.VerifyRenderingInServerForm (this);
323 bool need_span = ControlStyleCreated;
325 ControlStyle.AddAttributesToRender (w, this);
328 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
334 w.AddAttribute ("title", tt);
338 if (Attributes.Count > 0){
339 Attributes.AddAttributes (w);
344 w.RenderBeginTag (HtmlTextWriterTag.Span);
346 TextAlign align = TextAlign;
347 if (align == TextAlign.Right) {
348 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
349 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
350 w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
351 InternalAddAttributesToRender (w);
353 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
356 w.AddAttribute (HtmlTextWriterAttribute.Onclick,
357 Page.ClientScript.GetPostBackClientEvent (this, String.Empty));
358 w.AddAttribute ("language", "javascript");
361 if (AccessKey.Length > 0)
362 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
365 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
366 TabIndex.ToString (CultureInfo.InvariantCulture));
368 w.RenderBeginTag (HtmlTextWriterTag.Input);
373 if (labelAttributes != null)
374 labelAttributes.AddAttributes (w);
376 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
377 w.RenderBeginTag (HtmlTextWriterTag.Label);
385 if (labelAttributes != null)
386 labelAttributes.AddAttributes (w);
388 w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
389 w.RenderBeginTag (HtmlTextWriterTag.Label);
395 w.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
397 w.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
398 w.AddAttribute (HtmlTextWriterAttribute.Type, render_type);
399 w.AddAttribute (HtmlTextWriterAttribute.Name, NameAttribute);
400 InternalAddAttributesToRender (w);
402 w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
405 w.AddAttribute (HtmlTextWriterAttribute.Onclick,
406 Page.ClientScript.GetPostBackClientEvent (this, String.Empty));
407 w.AddAttribute ("language", "javascript");
410 if (AccessKey.Length > 0)
411 w.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
414 w.AddAttribute (HtmlTextWriterAttribute.Tabindex,
415 TabIndex.ToString (NumberFormatInfo.InvariantInfo));
417 w.RenderBeginTag (HtmlTextWriterTag.Input);
428 bool LoadPostData (string postDataKey, NameValueCollection postCollection)
430 string postedValue = postCollection[postDataKey];
431 bool postedBool = ((postedValue != null) &&
432 (postedValue.Length > 0));
434 if (Checked != postedBool) {
435 Checked = postedBool;
445 void RaisePostDataChangedEvent ()
448 if (CausesValidation)
449 Page.Validate (ValidationGroup);
451 OnCheckedChanged (EventArgs.Empty);
454 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
456 return LoadPostData (postDataKey, postCollection);
459 void IPostBackDataHandler.RaisePostDataChangedEvent ()
461 RaisePostDataChangedEvent ();
465 protected override void AddAttributesToRender (HtmlTextWriter writer)
467 base.AddAttributesToRender (writer);
471 internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)