HtmlInputCheckBox.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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)
  37. handler (this, e);
  38. }
  39. protected override void OnPreRender(EventArgs e){
  40. if (Page != null && !Disabled)
  41. Page.RegisterRequiresPostBack(this);
  42. if (Events[EventServerChange] != null && !Disabled)
  43. ViewState.SetItemDirty("checkbox",false);
  44. }
  45. [WebCategory("Action")]
  46. [WebSysDescription("Fires when the checked satte of the control is changed.")]
  47. public event EventHandler ServerChange{
  48. add{
  49. Events.AddHandler(EventServerChange, value);
  50. }
  51. remove{
  52. Events.RemoveHandler(EventServerChange, value);
  53. }
  54. }
  55. [DefaultValue("")]
  56. [WebCategory("Misc")]
  57. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  58. public bool Checked{
  59. get{
  60. string attr = Attributes["checked"];
  61. if (attr != null)
  62. return attr.Equals("checked");
  63. return false;
  64. }
  65. set{
  66. Attributes["checked"] = (value == true)? "checked": null;
  67. }
  68. }
  69. } // class HtmlInputCheckBox
  70. } // namespace System.Web.UI.HtmlControls