2 // System.Web.UI.WebControls.CheckBox.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
10 // Thanks to Leen Toelen (toelen@hotmail.com)'s classes that helped me
11 // to write the contents of the function LoadPostData(...)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
37 using System.Collections.Specialized;
38 using System.Globalization;
41 using System.ComponentModel;
42 using System.ComponentModel.Design;
44 namespace System.Web.UI.WebControls
47 [ControlValuePropertyAttribute ("Checked")]
49 [DefaultEvent("CheckedChanged")]
50 [DefaultProperty("Text")]
51 [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
52 [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
53 public class CheckBox : WebControl, IPostBackDataHandler
58 private static readonly object CheckedChangedEvent = new object();
59 AttributeCollection commonAttrs;
62 AttributeCollection inputAttributes;
63 StateBag inputAttributesState;
64 AttributeCollection labelAttributes;
65 StateBag labelAttributesState;
68 public CheckBox(): base(HtmlTextWriterTag.Input)
73 [ThemeableAttribute (false)]
75 [DefaultValue (false), WebCategory ("Behavior")]
76 [WebSysDescription ("The control automatically posts back after changing the text.")]
77 public virtual bool AutoPostBack
80 object o = ViewState ["AutoPostBack"];
81 return (o == null) ? false : (bool) o;
84 set { ViewState ["AutoPostBack"] = value; }
89 [ThemeableAttribute (false)]
91 [DefaultValue (false), Bindable (true)]
92 [WebSysDescription ("Determines if the control is checked.")]
93 public virtual bool Checked
96 object o = ViewState ["Checked"];
97 return (o == null) ? false : (bool) o;
100 set { ViewState ["Checked"] = value; }
106 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
107 [WebSysDescription ("The text that this control displays.")]
108 public virtual string Text
111 object o = ViewState ["Text"];
112 return (o == null) ? String.Empty : (string) o;
115 set { ViewState ["Text"] = value; }
118 private bool SaveCheckedViewState
121 if (Events [CheckedChangedEvent] != null || !Enabled)
124 Type type = GetType ();
125 return (type != typeof (CheckBox) && type != typeof (RadioButton));
132 [DefaultValue (typeof (TextAlign), "Right"), WebCategory ("Appearance")]
133 [WebSysDescription ("The alignment of the text.")]
134 public virtual TextAlign TextAlign
137 object o = ViewState ["TextAlign"];
138 return (o == null) ? TextAlign.Right : (TextAlign) o;
142 if (!System.Enum.IsDefined (typeof (TextAlign), value))
143 throw new ArgumentException ();
144 ViewState ["TextAlign"] = value;
149 [ThemeableAttribute (false)]
150 [DefaultValue (false), WebCategory ("Behavior")]
151 public bool CausesValidation
155 Object cv = ViewState["CausesValidation"];
162 ViewState["CausesValidation"] = value;
166 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
167 public AttributeCollection InputAttributes
170 if (inputAttributes == null) {
171 if (inputAttributesState == null) {
172 inputAttributesState = new StateBag (true);
173 if (IsTrackingViewState)
174 inputAttributesState.TrackViewState();
176 inputAttributes = new AttributeCollection (inputAttributesState);
178 return inputAttributes;
182 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
183 public AttributeCollection LabelAttributes
186 if (labelAttributes == null) {
187 if (labelAttributesState == null) {
188 labelAttributesState = new StateBag (true);
189 if (IsTrackingViewState)
190 labelAttributesState.TrackViewState();
192 labelAttributes = new AttributeCollection (labelAttributesState);
194 return labelAttributes;
198 [DefaultValueAttribute ("")]
199 [ThemeableAttribute (false)]
200 [WebCategoryAttribute ("Behavior")]
201 public string ValidationGroup {
203 string text = (string)ViewState["ValidationGroup"];
204 if (text!=null) return text;
208 ViewState["ValidationGroup"] = value;
214 [WebCategory ("Action")]
215 [WebSysDescription ("Raised when the control is checked or unchecked.")]
216 public event EventHandler CheckedChanged
218 add { Events.AddHandler (CheckedChangedEvent, value); }
219 remove { Events.RemoveHandler (CheckedChangedEvent, value); }
222 protected virtual void OnCheckedChanged(EventArgs e)
225 EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
232 protected override void TrackViewState ()
234 base.TrackViewState();
235 if (inputAttributesState != null)
236 inputAttributesState.TrackViewState ();
237 if (labelAttributesState != null)
238 labelAttributesState.TrackViewState ();
241 protected override object SaveViewState ()
243 object baseView = base.SaveViewState ();
244 object inputAttrView = null;
245 object labelAttrView = null;
247 if (inputAttributesState != null)
248 inputAttrView = inputAttributesState.SaveViewState ();
250 if (labelAttributesState != null)
251 labelAttrView = labelAttributesState.SaveViewState ();
253 if (baseView == null && inputAttrView == null && labelAttrView == null)
256 return new Triplet (baseView, inputAttrView, labelAttrView);
259 protected override void LoadViewState (object savedState)
261 if (savedState == null)
264 Triplet saved = (Triplet) savedState;
265 base.LoadViewState (saved.First);
267 if (saved.Second != null) {
268 if (inputAttributesState == null) {
269 inputAttributesState = new StateBag(true);
270 inputAttributesState.TrackViewState ();
272 inputAttributesState.LoadViewState (saved.Second);
275 if (saved.Third != null) {
276 if (labelAttributesState == null) {
277 labelAttributesState = new StateBag(true);
278 labelAttributesState.TrackViewState ();
280 labelAttributesState.LoadViewState (saved.Third);
285 protected override void OnPreRender(EventArgs e)
287 if (Page != null && Enabled) {
288 Page.RegisterRequiresPostBack (this);
290 Page.RequiresPostBackScript ();
293 if (!SaveCheckedViewState)
294 ViewState.SetItemDirty ("Checked", false);
297 static bool IsInputOrCommonAttr (string attname)
332 void AddAttributesForSpan (HtmlTextWriter writer)
334 ICollection k = Attributes.Keys;
335 string [] keys = new string [k.Count];
337 foreach (string key in keys) {
338 if (!IsInputOrCommonAttr (key.ToUpper ()))
341 if (commonAttrs == null)
342 commonAttrs = new AttributeCollection (new StateBag ());
344 commonAttrs [key] = Attributes [key];
345 Attributes.Remove (key);
348 Attributes.AddAttributes (writer);
352 protected override void AddAttributesToRender (HtmlTextWriter writer)
354 base.AddAttributesToRender (writer);
358 protected override void Render (HtmlTextWriter writer)
360 bool hasBeginRendering = false;
361 if(ControlStyleCreated && !ControlStyle.IsEmpty){
362 hasBeginRendering = true;
363 ControlStyle.AddAttributesToRender (writer, this);
368 hasBeginRendering = true;
\r
369 writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
\r
372 if (ToolTip.Length > 0){
373 hasBeginRendering = true;
374 writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
377 if (Attributes.Count > 0){
378 hasBeginRendering = true;
379 AddAttributesForSpan (writer);
382 if (hasBeginRendering)
383 writer.RenderBeginTag (HtmlTextWriterTag.Span);
385 if (Text.Length > 0){
386 TextAlign ta = TextAlign;
387 if(ta == TextAlign.Right) {
388 if (commonAttrs != null)
389 commonAttrs.AddAttributes (writer);
390 RenderInputTag (writer, ClientID);
393 if (labelAttributes != null)
394 labelAttributes.AddAttributes (writer);
396 writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
397 writer.RenderBeginTag (HtmlTextWriterTag.Label);
399 writer.RenderEndTag ();
400 if(ta == TextAlign.Left) {
401 if (commonAttrs != null)
402 commonAttrs.AddAttributes (writer);
403 RenderInputTag (writer, ClientID);
406 if (commonAttrs != null)
407 commonAttrs.AddAttributes (writer);
408 RenderInputTag (writer, ClientID);
411 if (hasBeginRendering)
412 writer.RenderEndTag ();
415 internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
418 if (inputAttributes != null)
419 inputAttributes.AddAttributes (writer);
422 writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
424 writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
425 writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
426 writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
428 writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
431 writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
432 Page.ClientScript.GetPostBackClientEvent (this, String.Empty));
433 writer.AddAttribute ("language", "javascript");
436 if (AccessKey.Length > 0)
437 writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
440 writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
441 TabIndex.ToString (NumberFormatInfo.InvariantInfo));
443 writer.RenderBeginTag (HtmlTextWriterTag.Input);
444 writer.RenderEndTag ();
448 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
450 return LoadPostData (postDataKey, postCollection);
453 protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
455 bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
458 string postedVal = postCollection [postDataKey];
\r
459 bool haveData = ((postedVal != null)&& (postedVal.Length > 0));
\r
460 bool diff = (haveData != Checked);
466 void IPostBackDataHandler.RaisePostDataChangedEvent()
468 RaisePostDataChangedEvent ();
471 protected virtual void RaisePostDataChangedEvent()
473 if (CausesValidation)
474 Page.Validate (ValidationGroup);
476 OnCheckedChanged (EventArgs.Empty);
479 void IPostBackDataHandler.RaisePostDataChangedEvent()
481 OnCheckedChanged (EventArgs.Empty);