2
0

CheckBox.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. public CheckBox(): base(HtmlTextWriterTag.Input)
  51. {
  52. }
  53. [DefaultValue (false), WebCategory ("Behavior")]
  54. [WebSysDescription ("The control automatically posts back after changing the text.")]
  55. public virtual bool AutoPostBack
  56. {
  57. get {
  58. object o = ViewState ["AutoPostBack"];
  59. return (o == null) ? false : (bool) o;
  60. }
  61. set { ViewState ["AutoPostBack"] = value; }
  62. }
  63. [DefaultValue (false), Bindable (true)]
  64. [WebSysDescription ("Determines if the control is checked.")]
  65. public virtual bool Checked
  66. {
  67. get {
  68. object o = ViewState ["Checked"];
  69. return (o == null) ? false : (bool) o;
  70. }
  71. set { ViewState ["Checked"] = value; }
  72. }
  73. [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
  74. [WebSysDescription ("The text that this control displays.")]
  75. public virtual string Text
  76. {
  77. get {
  78. object o = ViewState ["Text"];
  79. return (o == null) ? String.Empty : (string) o;
  80. }
  81. set { ViewState ["Text"] = value; }
  82. }
  83. private bool SaveCheckedViewState
  84. {
  85. get {
  86. if (Events [CheckedChangedEvent] != null || !Enabled)
  87. return true;
  88. Type type = GetType ();
  89. return (type != typeof (CheckBox) && type != typeof (RadioButton));
  90. }
  91. }
  92. [DefaultValue (typeof (TextAlign), "Right"), Bindable (true), WebCategory ("Appearance")]
  93. [WebSysDescription ("The alignment of the text.")]
  94. public virtual TextAlign TextAlign
  95. {
  96. get {
  97. object o = ViewState ["TextAlign"];
  98. return (o == null) ? TextAlign.Right : (TextAlign) o;
  99. }
  100. set {
  101. if (!System.Enum.IsDefined (typeof (TextAlign), value))
  102. throw new ArgumentException ();
  103. ViewState ["TextAlign"] = value;
  104. }
  105. }
  106. [WebCategory ("Action")]
  107. [WebSysDescription ("Raised when the control is checked or unchecked.")]
  108. public event EventHandler CheckedChanged
  109. {
  110. add { Events.AddHandler (CheckedChangedEvent, value); }
  111. remove { Events.RemoveHandler (CheckedChangedEvent, value); }
  112. }
  113. protected virtual void OnCheckedChanged(EventArgs e)
  114. {
  115. if(Events != null){
  116. EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
  117. if(eh != null)
  118. eh (this, e);
  119. }
  120. }
  121. protected override void OnPreRender(EventArgs e)
  122. {
  123. if (Page != null && Enabled) {
  124. Page.RegisterRequiresPostBack (this);
  125. if (AutoPostBack)
  126. Page.RequiresPostBackScript ();
  127. }
  128. if (!SaveCheckedViewState)
  129. ViewState.SetItemDirty ("Checked", false);
  130. }
  131. protected override void Render (HtmlTextWriter writer)
  132. {
  133. bool hasBeginRendering = false;
  134. if(ControlStyleCreated && !ControlStyle.IsEmpty){
  135. hasBeginRendering = true;
  136. ControlStyle.AddAttributesToRender (writer, this);
  137. }
  138. if (!Enabled)
  139. {
  140. hasBeginRendering = true;
  141. writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
  142. }
  143. if (ToolTip.Length > 0){
  144. hasBeginRendering = true;
  145. writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
  146. }
  147. if (Attributes.Count > 0){
  148. string val = Attributes ["value"];
  149. Attributes.Remove ("value");
  150. if (Attributes.Count > 0){
  151. hasBeginRendering = true;
  152. Attributes.AddAttributes (writer);
  153. }
  154. if (val != null)
  155. Attributes ["value"] = val;
  156. }
  157. if (hasBeginRendering)
  158. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  159. if (Text.Length > 0){
  160. TextAlign ta = TextAlign;
  161. if(ta == TextAlign.Right)
  162. RenderInputTag (writer, ClientID);
  163. writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
  164. writer.RenderBeginTag (HtmlTextWriterTag.Label);
  165. writer.Write (Text);
  166. writer.RenderEndTag ();
  167. if(ta == TextAlign.Left)
  168. RenderInputTag (writer, ClientID);
  169. }
  170. else
  171. RenderInputTag (writer, ClientID);
  172. if (hasBeginRendering)
  173. writer.RenderEndTag ();
  174. }
  175. internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
  176. {
  177. if (!Enabled)
  178. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
  179. writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
  180. writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
  181. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  182. if (Checked)
  183. writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
  184. if (AutoPostBack){
  185. writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
  186. Page.GetPostBackClientEvent (this, String.Empty));
  187. writer.AddAttribute ("language", "javascript");
  188. }
  189. if (AccessKey.Length > 0)
  190. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  191. if (TabIndex != 0)
  192. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
  193. TabIndex.ToString (NumberFormatInfo.InvariantInfo));
  194. writer.RenderBeginTag (HtmlTextWriterTag.Input);
  195. writer.RenderEndTag ();
  196. }
  197. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  198. {
  199. string postedVal = postCollection [postDataKey];
  200. bool haveData = ((postedVal != null)&& (postedVal.Length > 0));
  201. bool diff = (haveData != Checked);
  202. Checked = haveData;
  203. return diff ;
  204. }
  205. void IPostBackDataHandler.RaisePostDataChangedEvent()
  206. {
  207. OnCheckedChanged (EventArgs.Empty);
  208. }
  209. }
  210. }