CheckBox.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. public class CheckBox : WebControl, IPostBackDataHandler
  29. {
  30. private static readonly object CheckedChangedEvent = new object();
  31. public CheckBox(): base(HtmlTextWriterTag.Input)
  32. {
  33. }
  34. public virtual bool AutoPostBack
  35. {
  36. get {
  37. object o = ViewState ["AutoPostBack"];
  38. return (o == null) ? false : (bool) o;
  39. }
  40. set { ViewState ["AutoPostBack"] = value; }
  41. }
  42. public virtual bool Checked
  43. {
  44. get {
  45. object o = ViewState ["Checked"];
  46. return (o == null) ? false : (bool) o;
  47. }
  48. set { ViewState ["Checked"] = value; }
  49. }
  50. public virtual string Text
  51. {
  52. get {
  53. object o = ViewState ["Text"];
  54. return (o == null) ? String.Empty : (string) o;
  55. }
  56. set { ViewState ["Text"] = value; }
  57. }
  58. private bool SaveCheckedViewState
  59. {
  60. get {
  61. if (Events [CheckedChangedEvent] != null){
  62. if (!Enabled)
  63. return true;
  64. Type type = GetType ();
  65. if (type == typeof (CheckBox))
  66. return false;
  67. if (type == typeof (RadioButton))
  68. return false;
  69. }
  70. return true;
  71. }
  72. }
  73. public virtual TextAlign TextAlign
  74. {
  75. get {
  76. object o = ViewState ["TextAlign"];
  77. return (o == null) ? TextAlign.Right : (TextAlign) o;
  78. }
  79. set {
  80. if (!System.Enum.IsDefined (typeof (TextAlign), value))
  81. throw new ArgumentException ();
  82. ViewState ["TextAlign"] = value;
  83. }
  84. }
  85. public event EventHandler CheckedChanged
  86. {
  87. add { Events.AddHandler (CheckedChangedEvent, value); }
  88. remove { Events.RemoveHandler (CheckedChangedEvent, value); }
  89. }
  90. protected virtual void OnCheckedChanged(EventArgs e)
  91. {
  92. if(Events != null){
  93. EventHandler eh = (EventHandler) (Events [CheckedChangedEvent]);
  94. if(eh != null)
  95. eh (this, e);
  96. }
  97. }
  98. protected override void OnPreRender(EventArgs e)
  99. {
  100. if (Page != null && Enabled)
  101. Page.RegisterRequiresPostBack (this);
  102. if (SaveCheckedViewState)
  103. ViewState.SetItemDirty ("checked", false);
  104. }
  105. protected override void Render (HtmlTextWriter writer)
  106. {
  107. bool hasBeginRendering = false;
  108. if(ControlStyleCreated && !ControlStyle.IsEmpty){
  109. hasBeginRendering = true;
  110. ControlStyle.AddAttributesToRender (writer, this);
  111. }
  112. if (!Enabled){
  113. hasBeginRendering = true;
  114. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
  115. }
  116. if (ToolTip.Length > 0){
  117. hasBeginRendering = true;
  118. writer.AddAttribute (HtmlTextWriterAttribute.Title, ToolTip);
  119. }
  120. if (Attributes.Count > 0){
  121. string val = Attributes ["value"];
  122. Attributes.Remove ("value");
  123. if (Attributes.Count > 0){
  124. hasBeginRendering = true;
  125. Attributes.AddAttributes (writer);
  126. }
  127. if (val != null)
  128. Attributes ["value"] = val;
  129. }
  130. if (hasBeginRendering)
  131. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  132. if (Text.Length > 0){
  133. TextAlign ta = TextAlign;
  134. if(ta == TextAlign.Right)
  135. RenderInputTag (writer, ClientID);
  136. writer.AddAttribute (HtmlTextWriterAttribute.For, ClientID);
  137. writer.RenderBeginTag (HtmlTextWriterTag.Label);
  138. writer.Write (Text);
  139. writer.RenderEndTag ();
  140. if(ta == TextAlign.Left)
  141. RenderInputTag (writer, ClientID);
  142. }
  143. else
  144. RenderInputTag (writer, ClientID);
  145. if (hasBeginRendering)
  146. writer.RenderEndTag ();
  147. }
  148. internal virtual void RenderInputTag (HtmlTextWriter writer, string clientId)
  149. {
  150. writer.AddAttribute (HtmlTextWriterAttribute.Id, clientId);
  151. writer.AddAttribute( HtmlTextWriterAttribute.Type, "checkbox");
  152. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueID);
  153. if (Checked)
  154. writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
  155. if (AutoPostBack){
  156. writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
  157. Page.GetPostBackClientEvent (this, String.Empty));
  158. writer.AddAttribute ("language", "javascript");
  159. }
  160. if (AccessKey.Length > 0)
  161. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  162. if (TabIndex != 0)
  163. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
  164. TabIndex.ToString (NumberFormatInfo.InvariantInfo));
  165. writer.RenderBeginTag (HtmlTextWriterTag.Input);
  166. writer.RenderEndTag ();
  167. }
  168. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  169. {
  170. string postedVal = postCollection [postDataKey];
  171. bool postChecked = false;
  172. if(postedVal != null)
  173. postChecked = postedVal.Length > 0;
  174. Checked = postChecked;
  175. return (postChecked == false);
  176. }
  177. void IPostBackDataHandler.RaisePostDataChangedEvent()
  178. {
  179. OnCheckedChanged (EventArgs.Empty);
  180. }
  181. }
  182. }