2
0

HtmlInputRadioButton.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 HtmlInputRadioButton : HtmlInputControl, IPostBackDataHandler{
  14. private static readonly object EventServerChange;
  15. public HtmlInputRadioButton(): base("radio"){}
  16. protected override void OnPreRender(EventArgs e){
  17. if (Page != null && !Disabled){
  18. Page.RegisterRequiresPostBack(this);
  19. }
  20. if (Events[EventServerChange] != null && !Disabled){
  21. ViewState.SetItemDirty("checked", false);
  22. }
  23. }
  24. protected virtual void OnServerChange(EventArgs e){
  25. EventHandler handler = (EventHandler) Events[EventServerChange];
  26. if (handler != null){
  27. handler.Invoke(this, e);
  28. }
  29. }
  30. protected override void RenderAttributes(HtmlTextWriter writer){
  31. writer.WriteAttribute("value", Value);
  32. Attributes.Remove("value");
  33. base.RenderAttributes(writer);
  34. }
  35. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  36. NameValueCollection postCollection)
  37. {
  38. string postValue = postCollection [postDataKey];
  39. bool myBool = false;
  40. if (postValue != null && postValue.Equals (Value)) {
  41. if (!Checked) {
  42. Checked = true;
  43. myBool = true;
  44. }
  45. } else {
  46. if (Checked) {
  47. Checked = false;
  48. myBool = false;
  49. }
  50. }
  51. return myBool;
  52. }
  53. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  54. {
  55. OnServerChange (EventArgs.Empty);
  56. }
  57. public event EventHandler ServerChange{
  58. add{
  59. Events.AddHandler(EventServerChange, value);
  60. }
  61. remove{
  62. Events.RemoveHandler(EventServerChange, value);
  63. }
  64. }
  65. public bool Checked{
  66. get{
  67. string attr = Attributes["checked"];
  68. if (attr != null){
  69. return attr.Equals("checked");
  70. }
  71. return false;
  72. }
  73. set{
  74. if (value != true){
  75. Attributes["checked"] = null;
  76. }
  77. Attributes["checked"] = "checked";
  78. }
  79. }
  80. public override string Name
  81. {
  82. get {
  83. string attr = Attributes ["name"]; // Gotta use "name" to group radio buttons
  84. return (attr == null) ? String.Empty : attr;
  85. }
  86. set { Attributes ["name"] = value; }
  87. }
  88. protected override string RenderedName{
  89. get{
  90. string attr = base.RenderedName;
  91. string id = UniqueID;
  92. int indexOfX = id.LastIndexOf('X');
  93. if (indexOfX != 0 && indexOfX >= 0){
  94. attr = String.Concat(attr, id.Substring(0,indexOfX+1));
  95. }
  96. return attr;
  97. }
  98. }
  99. public override string Value
  100. {
  101. get {
  102. string v = Attributes ["value"];
  103. if (v != null && v != "")
  104. return v;
  105. v = ID;
  106. Attributes ["value"] = v;
  107. return v;
  108. }
  109. set { Attributes ["value"] = value; }
  110. }
  111. } // class HtmlInputRadioButton
  112. } // namespace System.Web.UI.HtmlControls