HtmlInputRadioButton.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. 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 new string Name{
  77. get{
  78. string attr = Attributes["name"];
  79. if (attr != null){
  80. return attr;
  81. }
  82. return String.Empty;
  83. }
  84. set{
  85. Attributes["name"] = AttributeToString(value);
  86. }
  87. }
  88. private new 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. } // class HtmlInputRadioButton
  100. } // namespace System.Web.UI.HtmlControls