2
0

CheckBox.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: CheckBox
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. * Thanks to Leen Toelen ([email protected])'s classes that helped me
  13. * to write the contents of the function LoadPostData(...)
  14. */
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Specialized;
  18. using System.Globalization;
  19. using System.Web;
  20. using System.Web.UI;
  21. using System.ComponentModel;
  22. namespace System.Web.UI.WebControls
  23. {
  24. [DefaultEvent("CheckedChanged")]
  25. [DefaultProperty("Text")]
  26. //[DataBindingHanlder("??")]
  27. //[Designer("??")]
  28. [PersistChildren(false)]
  29. [ParseChildren(true)]
  30. public class CheckBox : WebControl, IPostBackDataHandler
  31. {
  32. private static readonly object CheckedChangedEvent = new object();
  33. public CheckBox(): base(HtmlTextWriterTag.Input)
  34. {
  35. }
  36. public virtual bool AutoPostBack
  37. {
  38. get {
  39. object o = ViewState ["AutoPostBack"];
  40. return (o == null) ? false : (bool) o;
  41. }
  42. set { ViewState ["AutoPostBack"] = value; }
  43. }
  44. public virtual bool Checked
  45. {
  46. get {
  47. object o = ViewState ["Checked"];
  48. return (o == null) ? false : (bool) o;
  49. }
  50. set { ViewState ["Checked"] = value; }
  51. }
  52. public virtual string Text
  53. {
  54. get {
  55. object o = ViewState ["Text"];
  56. return (o == null) ? String.Empty : (string) o;
  57. }
  58. set { ViewState ["Text"] = value; }
  59. }
  60. private bool SaveCheckedViewState
  61. {
  62. get {
  63. if (Events [CheckedChangedEvent] != null){
  64. if (!Enabled)
  65. return true;
  66. Type type = GetType ();
  67. if (type == typeof (CheckBox))
  68. return false;
  69. if (type == typeof (RadioButton))
  70. return false;
  71. }
  72. return true;
  73. }
  74. }
  75. public virtual TextAlign TextAlign
  76. {
  77. get {
  78. object o = ViewState ["TextAlign"];
  79. return (o == null) ? TextAlign.Right : (TextAlign) o;
  80. }
  81. set {
  82. if (!System.Enum.IsDefined (typeof (TextAlign), value))
  83. throw new ArgumentException ();
  84. ViewState ["TextAlign"] = value;
  85. }
  86. }
  87. public event EventHandler CheckedChanged
  88. {
  89. add { Events.AddHandler (CheckedChangedEvent, value); }
  90. remove { Events.RemoveHandler (CheckedChangedEvent, value); }
  91. }
  92. protected virtual void OnCheckedChanged(EventArgs e)
  93. {
  94. if(Events != null){
  95. EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
  96. if(eh != null)
  97. eh (this, e);
  98. }
  99. }
  100. protected override void OnPreRender(EventArgs e)
  101. {
  102. if (Page != null && Enabled)
  103. Page.RegisterRequiresPostBack (this);
  104. if (SaveCheckedViewState)
  105. ViewState.SetItemDirty ("checked", false);
  106. }
  107. protected override void Render (HtmlTextWriter writer)
  108. {
  109. bool hasBeginRendering = false;
  110. if(ControlStyleCreated && !ControlStyle.IsEmpty){
  111. hasBeginRendering = true;
  112. ControlStyle.AddAttributesToRender (writer, this);
  113. }
  114. if (!Enabled){
  115. hasBeginRendering = true;
  116. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
  117. }
  118. if (ToolTip.Length > 0){
  119. hasBeginRendering = true;
  120. writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
  121. }
  122. if (Attributes.Count > 0){
  123. string val = Attributes ["value"];
  124. Attributes.Remove ("value");
  125. if (Attributes.Count > 0){
  126. hasBeginRendering = true;
  127. Attributes.AddAttributes (writer);
  128. }
  129. if (val != null)
  130. Attributes ["value"] = val;
  131. }
  132. if (hasBeginRendering)
  133. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  134. if (Text.Length > 0){
  135. TextAlign ta = TextAlign;
  136. if(ta == TextAlign.Right)
  137. RenderInputTag (writer, ClientID);
  138. writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
  139. writer.RenderBeginTag (HtmlTextWriterTag.Label);
  140. writer.Write (Text);
  141. writer.RenderEndTag ();
  142. if(ta == TextAlign.Left)
  143. RenderInputTag (writer, ClientID);
  144. }
  145. else
  146. RenderInputTag (writer, ClientID);
  147. if (hasBeginRendering)
  148. writer.RenderEndTag ();
  149. }
  150. internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
  151. {
  152. writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
  153. writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
  154. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  155. if (Checked)
  156. writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
  157. if (AutoPostBack){
  158. writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
  159. Page.GetPostBackClientEvent (this, String.Empty));
  160. writer.AddAttribute ("language", "javascript");
  161. }
  162. if (AccessKey.Length > 0)
  163. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  164. if (TabIndex != 0)
  165. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
  166. TabIndex.ToString (NumberFormatInfo.InvariantInfo));
  167. writer.RenderBeginTag (HtmlTextWriterTag.Input);
  168. writer.RenderEndTag ();
  169. }
  170. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  171. {
  172. string postedVal = postCollection [postDataKey];
  173. bool postChecked = false;
  174. if(postedVal != null)
  175. postChecked = postedVal.Length > 0;
  176. Checked = postChecked;
  177. return (postChecked == false);
  178. }
  179. void IPostBackDataHandler.RaisePostDataChangedEvent()
  180. {
  181. OnCheckedChanged (EventArgs.Empty);
  182. }
  183. }
  184. }