| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- //
- // System.Web.UI.WebControls.CheckBox.cs
- //
- // Author:
- // Dick Porter <[email protected]>
- //
- // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System.Web.UI;
- using System.Collections.Specialized;
- using System.ComponentModel;
- namespace System.Web.UI.WebControls {
- [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
- [DataBindingHandler ("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
- [DefaultEvent ("CheckedChanged")]
- [DefaultProperty ("Text")]
- #if NET_2_0
- [ControlValueProperty ("Checked", null)]
- #endif
- public class CheckBox : WebControl, IPostBackDataHandler
- #if NET_2_0
- , ICheckBoxControl
- #endif
- {
- string render_type;
- #if NET_2_0
- AttributeCollection inputAttributes;
- StateBag inputAttributesState;
- AttributeCollection labelAttributes;
- StateBag labelAttributesState;
- #endif
-
- public CheckBox () : base (HtmlTextWriterTag.Input)
- {
- render_type = "checkbox";
- }
- internal CheckBox (string render_type) : base (HtmlTextWriterTag.Input)
- {
- this.render_type = render_type;
- }
- [DefaultValue (false)]
- #if NET_2_0
- [Themeable (false)]
- #endif
- [WebSysDescription ("")]
- [WebCategory ("Behavior")]
- public virtual bool AutoPostBack
- {
- get {
- return (ViewState.GetBool ("AutoPostBack",
- false));
- }
- set {
- ViewState["AutoPostBack"] = value;
- }
- }
- #if NET_2_0
- [DefaultValue (false)]
- [Themeable (false)]
- [MonoTODO]
- [WebSysDescription ("")]
- [WebCategory ("Behavior")]
- public virtual bool CausesValidation
- {
- get { return ViewState.GetBool ("CausesValidation", false); }
- set { ViewState ["CausesValidation"] = value; }
- }
- #endif
-
- [DefaultValue (false)]
- #if NET_2_0
- [Bindable (true, BindingDirection.TwoWay)]
- [Themeable (false)]
- #else
- [Bindable (true)]
- #endif
- [WebSysDescription ("")]
- [WebCategory ("Behavior")]
- public virtual bool Checked
- {
- get {
- return (ViewState.GetBool ("Checked", false));
- }
- set {
- ViewState["Checked"] = value;
- }
- }
- #if NET_2_0
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public AttributeCollection InputAttributes
- {
- get {
- if (inputAttributes == null) {
- if (inputAttributesState == null) {
- inputAttributesState = new StateBag (true);
- if (IsTrackingViewState)
- inputAttributesState.TrackViewState();
- }
- inputAttributes = new AttributeCollection (inputAttributesState);
- }
- return inputAttributes;
- }
- }
- [Browsable (false)]
- [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
- public AttributeCollection LabelAttributes
- {
- get {
- if (labelAttributes == null) {
- if (labelAttributesState == null) {
- labelAttributesState = new StateBag (true);
- if (IsTrackingViewState)
- labelAttributesState.TrackViewState();
- }
- labelAttributes = new AttributeCollection (labelAttributesState);
- }
- return labelAttributes;
- }
- }
- #endif
- [DefaultValue ("")]
- [Bindable (true)]
- #if NET_2_0
- [Localizable (true)]
- #endif
- [WebSysDescription ("")]
- [WebCategory ("Appearance")]
- public virtual string Text
- {
- get {
- return (ViewState.GetString ("Text",
- String.Empty));
- }
- set {
- ViewState["Text"] = value;
- }
- }
- [DefaultValue (TextAlign.Right)]
- #if ONLY_1_1
- [Bindable (true)]
- #endif
- [WebSysDescription ("")]
- [WebCategory ("Appearance")]
- public virtual TextAlign TextAlign
- {
- get {
- object o = ViewState["TextAlign"];
- if (o == null) {
- return (TextAlign.Right);
- } else {
- return ((TextAlign)o);
- }
- }
- set {
- if (value != TextAlign.Left &&
- value != TextAlign.Right) {
- throw new ArgumentOutOfRangeException ("value");
- }
-
- ViewState["TextAlign"] = value;
- }
- }
- #if NET_2_0
- [Themeable (false)]
- [DefaultValue ("")]
- [WebSysDescription ("")]
- [WebCategoryAttribute ("Behavior")]
- public string ValidationGroup
- {
- get { return ViewState.GetString ("ValidationGroup", String.Empty); }
- set { ViewState["ValidationGroup"] = value; }
- }
- #endif
- private static readonly object EventCheckedChanged = new object ();
- [WebSysDescription ("")]
- [WebCategory ("Action")]
- public event EventHandler CheckedChanged
- {
- add {
- Events.AddHandler (EventCheckedChanged, value);
- }
- remove {
- Events.RemoveHandler (EventCheckedChanged, value);
- }
- }
- protected virtual void OnCheckedChanged (EventArgs e)
- {
- EventHandler handler = (EventHandler)Events[EventCheckedChanged];
-
- if (handler != null) {
- handler (this, e);
- }
- }
- internal virtual string NameAttribute
- {
- get {
- return (this.UniqueID);
- }
- }
-
- protected override void AddAttributesToRender (HtmlTextWriter w)
- {
- if (Page != null)
- Page.VerifyRenderingInServerForm (this);
- /* This is a nasty kludge to avoid rendering
- * "style" and the ControlStyle in checkboxes
- * (we use a surrounding <span> instead), but
- * still pass the unit test that shows the
- * style being rendered in multiple calls to
- * Render () (which means we can't just delete
- * Attributes["style"] or ControlStyle.Reset()
- * in Render ())
- */
- string css_style = Attributes ["style"];
- if (css_style != null) {
- Attributes.Remove ("style");
- }
- Style style = new Style ();
- if (ControlStyleCreated) {
- style.CopyFrom (ControlStyle);
- ControlStyle.Reset ();
- }
-
- base.AddAttributesToRender (w);
- if (css_style != null) {
- Attributes ["style"] = css_style;
- }
- if (!style.IsEmpty) {
- ApplyStyle (style);
- }
-
- InternalAddAttributesToRender (w);
-
- w.AddAttribute (HtmlTextWriterAttribute.Type,
- render_type);
- w.AddAttribute (HtmlTextWriterAttribute.Name,
- NameAttribute);
-
- if (AutoPostBack) {
- w.AddAttribute (HtmlTextWriterAttribute.Onclick, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
- }
- if (Checked) {
- w.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
- }
- }
- #if NET_2_0
- protected override void LoadViewState (object savedState)
- {
- if (savedState == null) {
- base.LoadViewState (null);
- return;
- }
- Triplet saved = (Triplet) savedState;
- base.LoadViewState (saved.First);
- if (saved.Second != null) {
- if (inputAttributesState == null) {
- inputAttributesState = new StateBag(true);
- inputAttributesState.TrackViewState ();
- }
- inputAttributesState.LoadViewState (saved.Second);
- }
- if (saved.Third != null) {
- if (labelAttributesState == null) {
- labelAttributesState = new StateBag(true);
- labelAttributesState.TrackViewState ();
- }
- labelAttributesState.LoadViewState (saved.Third);
- }
- }
- protected override object SaveViewState ()
- {
- object baseView = base.SaveViewState ();
- object inputAttrView = null;
- object labelAttrView = null;
- if (inputAttributesState != null)
- inputAttrView = inputAttributesState.SaveViewState ();
- if (labelAttributesState != null)
- labelAttrView = labelAttributesState.SaveViewState ();
- if (baseView == null && inputAttrView == null && labelAttrView == null)
- return null;
- return new Triplet (baseView, inputAttrView, labelAttrView);
- }
- protected override void TrackViewState ()
- {
- base.TrackViewState();
- if (inputAttributesState != null)
- inputAttributesState.TrackViewState ();
- if (labelAttributesState != null)
- labelAttributesState.TrackViewState ();
- }
- #endif
- #if NET_2_0
- protected internal
- #else
- protected
- #endif
- override void OnPreRender (EventArgs e)
- {
- base.OnPreRender (e);
- if (Page != null) {
- Page.RegisterRequiresPostBack (this);
- }
- }
- void RenderLabel (HtmlTextWriter w)
- {
- if (Text.Length > 0) {
- #if NET_2_0
- if (labelAttributes != null)
- labelAttributes.AddAttributes (w);
- #endif
- w.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
- w.RenderBeginTag (HtmlTextWriterTag.Label);
- w.Write (this.Text);
- w.RenderEndTag ();
- }
- }
-
- #if NET_2_0
- protected internal
- #else
- protected
- #endif
- override void Render (HtmlTextWriter w)
- {
- bool control_style = false;
-
- /* Need to apply the styles around the text
- * label too
- */
- if (ControlStyleCreated) {
- ControlStyle.AddAttributesToRender (w, this);
- w.RenderBeginTag (HtmlTextWriterTag.Span);
- control_style = true;
- } else if (Attributes ["style"] != null) {
- /* TODO: check if this or the style
- * has precendence, or if they should
- * be merged (if I can figure out how
- * to turn a CssStyleCollection into a
- * Style)
- */
- CssStyleCollection style = Attributes.CssStyle;
-
- w.AddAttribute (HtmlTextWriterAttribute.Style,
- style.BagToString ());
- w.RenderBeginTag (HtmlTextWriterTag.Span);
- control_style = true;
- }
-
- if (TextAlign == TextAlign.Left) {
- RenderLabel (w);
- base.Render (w);
- } else {
- base.Render (w);
- RenderLabel (w);
- }
- if (control_style) {
- w.RenderEndTag ();
- }
- }
- #if NET_2_0
- protected virtual
- #endif
- bool LoadPostData (string postDataKey, NameValueCollection postCollection)
- {
- string postedValue = postCollection[postDataKey];
- bool postedBool = ((postedValue != null) &&
- (postedValue.Length > 0));
-
- if (Checked != postedBool) {
- Checked = postedBool;
- return (true);
- }
- return (false);
- }
- bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
- {
- return LoadPostData (postDataKey, postCollection);
- }
-
- #if NET_2_0
- protected virtual void RaisePostDataChangedEvent ()
- {
- if (CausesValidation)
- Page.Validate (ValidationGroup);
-
- OnCheckedChanged (EventArgs.Empty);
- }
-
- void IPostBackDataHandler.RaisePostDataChangedEvent ()
- {
- RaisePostDataChangedEvent ();
- }
- #else
- void IPostBackDataHandler.RaisePostDataChangedEvent ()
- {
- OnCheckedChanged (EventArgs.Empty);
- }
- #endif
- internal virtual void InternalAddAttributesToRender (HtmlTextWriter w)
- {
- }
- }
- }
|