HtmlInputCheckBox.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* System.Web.UI.HtmlControls
  2. * Authors
  3. * Leen Toelen ([email protected])
  4. */
  5. using System;
  6. using System.Collections.Specialized;
  7. using System.ComponentModel;
  8. using System.Globalization;
  9. using System.Web;
  10. using System.Web.UI;
  11. namespace System.Web.UI.HtmlControls{
  12. [DefaultEvent("ServerChange")]
  13. public class HtmlInputCheckBox : HtmlInputControl, IPostBackDataHandler{
  14. private static readonly object EventServerChange = new object ();
  15. public HtmlInputCheckBox(): base("checkbox"){}
  16. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  17. NameValueCollection postCollection)
  18. {
  19. string postValue = postCollection [postDataKey];
  20. bool postChecked = false;
  21. bool retval = false;
  22. if (postValue != null)
  23. postChecked = postValue.Length > 0;
  24. if (Checked != postChecked){
  25. retval = true;
  26. Checked = postChecked;
  27. }
  28. return retval;
  29. }
  30. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  31. {
  32. OnServerChange (EventArgs.Empty);
  33. }
  34. protected virtual void OnServerChange(EventArgs e){
  35. EventHandler handler = (EventHandler) Events[EventServerChange];
  36. if (handler != null) handler.Invoke(this, e);
  37. }
  38. protected override void OnPreRender(EventArgs e){
  39. if (Page != null && !Disabled)
  40. Page.RegisterRequiresPostBack(this);
  41. if (Events[EventServerChange] != null && !Disabled)
  42. ViewState.SetItemDirty("checkbox",false);
  43. }
  44. [WebCategory("Action")]
  45. [WebSysDescription("Fires when the checked satte of the control is changed.")]
  46. public event EventHandler ServerChange{
  47. add{
  48. Events.AddHandler(EventServerChange, value);
  49. }
  50. remove{
  51. Events.RemoveHandler(EventServerChange, value);
  52. }
  53. }
  54. [DefaultValue("")]
  55. [WebCategory("Misc")]
  56. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  57. public bool Checked{
  58. get{
  59. string attr = Attributes["checked"];
  60. if (attr != null)
  61. return attr.Equals("checked");
  62. return false;
  63. }
  64. set{
  65. Attributes["checked"] = (value == true)? "checked": null;
  66. }
  67. }
  68. } // class HtmlInputCheckBox
  69. } // namespace System.Web.UI.HtmlControls