HtmlInputRadioButton.cs 2.9 KB

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