HtmlInputCheckBox.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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;
  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. public event EventHandler ServerChange{
  45. add{
  46. Events.AddHandler(EventServerChange, value);
  47. }
  48. remove{
  49. Events.RemoveHandler(EventServerChange, value);
  50. }
  51. }
  52. public bool Checked{
  53. get{
  54. string attr = Attributes["checked"];
  55. if (attr != null)
  56. return attr.Equals("checked");
  57. return false;
  58. }
  59. set{
  60. Attributes["checked"] = (value == true)? "checked": null;
  61. }
  62. }
  63. } // class HtmlInputCheckBox
  64. } // namespace System.Web.UI.HtmlControls