| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- //
- // System.Web.UI.WebControls.CheckBox.cs
- //
- // Authors:
- // Gaurav Vaish ([email protected])
- // Andreas Nahr ([email protected])
- //
- // (C) Gaurav Vaish (2002)
- // (C) 2003 Andreas Nahr
- // Thanks to Leen Toelen ([email protected])'s classes that helped me
- // to write the contents of the function LoadPostData(...)
- //
- //
- // 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;
- using System.Collections;
- using System.Collections.Specialized;
- using System.Globalization;
- using System.Web;
- using System.Web.UI;
- using System.ComponentModel;
- using System.ComponentModel.Design;
- namespace System.Web.UI.WebControls
- {
- [DefaultEvent("CheckedChanged")]
- [DefaultProperty("Text")]
- [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
- [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
- public class CheckBox : WebControl, IPostBackDataHandler
- {
- private static readonly object CheckedChangedEvent = new object();
- AttributeCollection commonAttrs;
-
- public CheckBox(): base(HtmlTextWriterTag.Input)
- {
- }
- [DefaultValue (false), WebCategory ("Behavior")]
- [WebSysDescription ("The control automatically posts back after changing the text.")]
- public virtual bool AutoPostBack
- {
- get {
- object o = ViewState ["AutoPostBack"];
- return (o == null) ? false : (bool) o;
- }
- set { ViewState ["AutoPostBack"] = value; }
- }
- [DefaultValue (false), Bindable (true)]
- [WebSysDescription ("Determines if the control is checked.")]
- public virtual bool Checked
- {
- get {
- object o = ViewState ["Checked"];
- return (o == null) ? false : (bool) o;
- }
- set { ViewState ["Checked"] = value; }
- }
- [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
- [WebSysDescription ("The text that this control displays.")]
- public virtual string Text
- {
- get {
- object o = ViewState ["Text"];
- return (o == null) ? String.Empty : (string) o;
- }
- set { ViewState ["Text"] = value; }
- }
-
- private bool SaveCheckedViewState
- {
- get {
- if (Events [CheckedChangedEvent] != null || !Enabled)
- return true;
- Type type = GetType ();
- return (type != typeof (CheckBox) && type != typeof (RadioButton));
- }
- }
- [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
- [WebSysDescription ("The alignment of the text.")]
- public virtual TextAlign TextAlign
- {
- get {
- object o = ViewState ["TextAlign"];
- return (o == null) ? TextAlign.Right : (TextAlign) o;
- }
- set {
- if (!System.Enum.IsDefined (typeof (TextAlign), value))
- throw new ArgumentException ();
- ViewState ["TextAlign"] = value;
- }
- }
- [WebCategory ("Action")]
- [WebSysDescription ("Raised when the control is checked or unchecked.")]
- public event EventHandler CheckedChanged
- {
- add { Events.AddHandler (CheckedChangedEvent, value); }
- remove { Events.RemoveHandler (CheckedChangedEvent, value); }
- }
-
- protected virtual void OnCheckedChanged(EventArgs e)
- {
- if(Events != null){
- EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
- if(eh != null)
- eh (this, e);
- }
- }
-
- protected override void OnPreRender(EventArgs e)
- {
- if (Page != null && Enabled) {
- Page.RegisterRequiresPostBack (this);
- if (AutoPostBack)
- Page.RequiresPostBackScript ();
- }
- if (!SaveCheckedViewState)
- ViewState.SetItemDirty ("Checked", false);
- }
- static bool IsInputOrCommonAttr (string attname)
- {
- switch (attname) {
- case "VALUE":
- case "CHECKED":
- case "SIZE":
- case "MAXLENGTH":
- case "SRC":
- case "ALT":
- case "USEMAP":
- case "DISABLED":
- case "READONLY":
- case "ACCEPT":
- case "ACCESSKEY":
- case "TABINDEX":
- case "ONFOCUS":
- case "ONBLUR":
- case "ONSELECT":
- case "ONCHANGE":
- case "ONCLICK":
- case "ONDBLCLICK":
- case "ONMOUSEDOWN":
- case "ONMOUSEUP":
- case "ONMOUSEOVER":
- case "ONMOUSEMOVE":
- case "ONMOUSEOUT":
- case "ONKEYPRESS":
- case "ONKEYDOWN":
- case "ONKEYUP":
- return true;
- default:
- return false;
- }
- }
- void AddAttributesForSpan (HtmlTextWriter writer)
- {
- ICollection k = Attributes.Keys;
- string [] keys = new string [k.Count];
- k.CopyTo (keys, 0);
- foreach (string key in keys) {
- if (!IsInputOrCommonAttr (key.ToUpper ()))
- continue;
- if (commonAttrs == null)
- commonAttrs = new AttributeCollection (new StateBag ());
- commonAttrs [key] = Attributes [key];
- Attributes.Remove (key);
- }
- Attributes.AddAttributes (writer);
- }
- protected override void Render (HtmlTextWriter writer)
- {
- bool hasBeginRendering = false;
- if(ControlStyleCreated && !ControlStyle.IsEmpty){
- hasBeginRendering = true;
- ControlStyle.AddAttributesToRender (writer, this);
- }
-
- if (!Enabled)
- {
- hasBeginRendering = true;
- writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
- }
- if (ToolTip.Length > 0){
- hasBeginRendering = true;
- writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
- }
- if (Attributes.Count > 0){
- hasBeginRendering = true;
- AddAttributesForSpan (writer);
- }
- if (hasBeginRendering)
- writer.RenderBeginTag (HtmlTextWriterTag.Span);
- if (Text.Length > 0){
- TextAlign ta = TextAlign;
- if(ta == TextAlign.Right) {
- if (commonAttrs != null)
- commonAttrs.AddAttributes (writer);
- RenderInputTag (writer, ClientID);
- }
- writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
- writer.RenderBeginTag (HtmlTextWriterTag.Label);
- writer.Write (Text);
- writer.RenderEndTag ();
- if(ta == TextAlign.Left) {
- if (commonAttrs != null)
- commonAttrs.AddAttributes (writer);
- RenderInputTag (writer, ClientID);
- }
- } else {
- if (commonAttrs != null)
- commonAttrs.AddAttributes (writer);
- RenderInputTag (writer, ClientID);
- }
- if (hasBeginRendering)
- writer.RenderEndTag ();
- }
-
- internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
- {
- if (!Enabled)
- writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
- writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
- writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
- writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
- if (Checked)
- writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
- if (AutoPostBack){
- writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
- Page.GetPostBackClientEvent (this, String.Empty));
- writer.AddAttribute ("language", "javascript");
- }
- if (AccessKey.Length > 0)
- writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
- if (TabIndex != 0)
- writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
- TabIndex.ToString (NumberFormatInfo.InvariantInfo));
- writer.RenderBeginTag (HtmlTextWriterTag.Input);
- writer.RenderEndTag ();
- }
-
- bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
- {
- string postedVal = postCollection [postDataKey];
- bool haveData = ((postedVal != null)&& (postedVal.Length > 0));
- bool diff = (haveData != Checked);
- Checked = haveData;
- return diff ;
- }
-
- void IPostBackDataHandler.RaisePostDataChangedEvent()
- {
- OnCheckedChanged (EventArgs.Empty);
- }
- }
- }
|