CheckBox.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // System.Web.UI.WebControls.CheckBox.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. // Thanks to Leen Toelen ([email protected])'s classes that helped me
  11. // to write the contents of the function LoadPostData(...)
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Collections;
  35. using System.Collections.Specialized;
  36. using System.Globalization;
  37. using System.Web;
  38. using System.Web.UI;
  39. using System.ComponentModel;
  40. using System.ComponentModel.Design;
  41. namespace System.Web.UI.WebControls
  42. {
  43. [DefaultEvent("CheckedChanged")]
  44. [DefaultProperty("Text")]
  45. [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
  46. [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  47. public class CheckBox : WebControl, IPostBackDataHandler
  48. {
  49. private static readonly object CheckedChangedEvent = new object();
  50. AttributeCollection commonAttrs;
  51. public CheckBox(): base(HtmlTextWriterTag.Input)
  52. {
  53. }
  54. [DefaultValue (false), WebCategory ("Behavior")]
  55. [WebSysDescription ("The control automatically posts back after changing the text.")]
  56. public virtual bool AutoPostBack
  57. {
  58. get {
  59. object o = ViewState ["AutoPostBack"];
  60. return (o == null) ? false : (bool) o;
  61. }
  62. set { ViewState ["AutoPostBack"] = value; }
  63. }
  64. [DefaultValue (false), Bindable (true)]
  65. [WebSysDescription ("Determines if the control is checked.")]
  66. public virtual bool Checked
  67. {
  68. get {
  69. object o = ViewState ["Checked"];
  70. return (o == null) ? false : (bool) o;
  71. }
  72. set { ViewState ["Checked"] = value; }
  73. }
  74. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  75. [WebSysDescription ("The text that this control displays.")]
  76. public virtual string Text
  77. {
  78. get {
  79. object o = ViewState ["Text"];
  80. return (o == null) ? String.Empty : (string) o;
  81. }
  82. set { ViewState ["Text"] = value; }
  83. }
  84. private bool SaveCheckedViewState
  85. {
  86. get {
  87. if (Events [CheckedChangedEvent] != null || !Enabled)
  88. return true;
  89. Type type = GetType ();
  90. return (type != typeof (CheckBox) && type != typeof (RadioButton));
  91. }
  92. }
  93. [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
  94. [WebSysDescription ("The alignment of the text.")]
  95. public virtual TextAlign TextAlign
  96. {
  97. get {
  98. object o = ViewState ["TextAlign"];
  99. return (o == null) ? TextAlign.Right : (TextAlign) o;
  100. }
  101. set {
  102. if (!System.Enum.IsDefined (typeof (TextAlign), value))
  103. throw new ArgumentException ();
  104. ViewState ["TextAlign"] = value;
  105. }
  106. }
  107. [WebCategory ("Action")]
  108. [WebSysDescription ("Raised when the control is checked or unchecked.")]
  109. public event EventHandler CheckedChanged
  110. {
  111. add { Events.AddHandler (CheckedChangedEvent, value); }
  112. remove { Events.RemoveHandler (CheckedChangedEvent, value); }
  113. }
  114. protected virtual void OnCheckedChanged(EventArgs e)
  115. {
  116. if(Events != null){
  117. EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
  118. if(eh != null)
  119. eh (this, e);
  120. }
  121. }
  122. protected override void OnPreRender(EventArgs e)
  123. {
  124. if (Page != null && Enabled) {
  125. Page.RegisterRequiresPostBack (this);
  126. if (AutoPostBack)
  127. Page.RequiresPostBackScript ();
  128. }
  129. if (!SaveCheckedViewState)
  130. ViewState.SetItemDirty ("Checked", false);
  131. }
  132. static bool IsInputOrCommonAttr (string attname)
  133. {
  134. switch (attname) {
  135. case "VALUE":
  136. case "CHECKED":
  137. case "SIZE":
  138. case "MAXLENGTH":
  139. case "SRC":
  140. case "ALT":
  141. case "USEMAP":
  142. case "DISABLED":
  143. case "READONLY":
  144. case "ACCEPT":
  145. case "ACCESSKEY":
  146. case "TABINDEX":
  147. case "ONFOCUS":
  148. case "ONBLUR":
  149. case "ONSELECT":
  150. case "ONCHANGE":
  151. case "ONCLICK":
  152. case "ONDBLCLICK":
  153. case "ONMOUSEDOWN":
  154. case "ONMOUSEUP":
  155. case "ONMOUSEOVER":
  156. case "ONMOUSEMOVE":
  157. case "ONMOUSEOUT":
  158. case "ONKEYPRESS":
  159. case "ONKEYDOWN":
  160. case "ONKEYUP":
  161. return true;
  162. default:
  163. return false;
  164. }
  165. }
  166. void AddAttributesForSpan (HtmlTextWriter writer)
  167. {
  168. ICollection k = Attributes.Keys;
  169. string [] keys = new string [k.Count];
  170. k.CopyTo (keys, 0);
  171. foreach (string key in keys) {
  172. if (!IsInputOrCommonAttr (key.ToUpper ()))
  173. continue;
  174. if (commonAttrs == null)
  175. commonAttrs = new AttributeCollection (new StateBag ());
  176. commonAttrs [key] = Attributes [key];
  177. Attributes.Remove (key);
  178. }
  179. Attributes.AddAttributes (writer);
  180. }
  181. protected override void Render (HtmlTextWriter writer)
  182. {
  183. bool hasBeginRendering = false;
  184. if(ControlStyleCreated && !ControlStyle.IsEmpty){
  185. hasBeginRendering = true;
  186. ControlStyle.AddAttributesToRender (writer, this);
  187. }
  188. if (!Enabled)
  189. {
  190. hasBeginRendering = true;
  191. writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  192. }
  193. if (ToolTip.Length > 0){
  194. hasBeginRendering = true;
  195. writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
  196. }
  197. if (Attributes.Count > 0){
  198. hasBeginRendering = true;
  199. AddAttributesForSpan (writer);
  200. }
  201. if (hasBeginRendering)
  202. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  203. if (Text.Length > 0){
  204. TextAlign ta = TextAlign;
  205. if(ta == TextAlign.Right) {
  206. if (commonAttrs != null)
  207. commonAttrs.AddAttributes (writer);
  208. RenderInputTag (writer, ClientID);
  209. }
  210. writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
  211. writer.RenderBeginTag (HtmlTextWriterTag.Label);
  212. writer.Write (Text);
  213. writer.RenderEndTag ();
  214. if(ta == TextAlign.Left) {
  215. if (commonAttrs != null)
  216. commonAttrs.AddAttributes (writer);
  217. RenderInputTag (writer, ClientID);
  218. }
  219. } else {
  220. if (commonAttrs != null)
  221. commonAttrs.AddAttributes (writer);
  222. RenderInputTag (writer, ClientID);
  223. }
  224. if (hasBeginRendering)
  225. writer.RenderEndTag ();
  226. }
  227. internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
  228. {
  229. if (!Enabled)
  230. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
  231. writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
  232. writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
  233. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  234. if (Checked)
  235. writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
  236. if (AutoPostBack){
  237. writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
  238. Page.GetPostBackClientEvent (this, String.Empty));
  239. writer.AddAttribute ("language", "javascript");
  240. }
  241. if (AccessKey.Length > 0)
  242. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  243. if (TabIndex != 0)
  244. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
  245. TabIndex.ToString (NumberFormatInfo.InvariantInfo));
  246. writer.RenderBeginTag (HtmlTextWriterTag.Input);
  247. writer.RenderEndTag ();
  248. }
  249. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  250. {
  251. string postedVal = postCollection [postDataKey];
  252. bool haveData = ((postedVal != null)&& (postedVal.Length > 0));
  253. bool diff = (haveData != Checked);
  254. Checked = haveData;
  255. return diff ;
  256. }
  257. void IPostBackDataHandler.RaisePostDataChangedEvent()
  258. {
  259. OnCheckedChanged (EventArgs.Empty);
  260. }
  261. }
  262. }