2
0

CheckBox.cs 5.8 KB

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