CheckBox.cs 6.0 KB

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