RadioButton.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: RadioButton
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Specialized;
  16. using System.Globalization;
  17. using System.Web;
  18. using System.Web.UI;
  19. namespace System.Web.UI.WebControls
  20. {
  21. public class RadioButton: CheckBox, IPostBackDataHandler
  22. {
  23. public RadioButton(): base()
  24. {
  25. }
  26. public virtual string GroupName
  27. {
  28. get
  29. {
  30. object o = ViewState["GroupName"];
  31. if(o != null)
  32. return (string)o;
  33. return String.Empty;
  34. }
  35. set
  36. {
  37. ViewState["GroupName"] = value;
  38. }
  39. }
  40. protected override void OnPreRender(EventArgs e)
  41. {
  42. base.OnPreRender(e);
  43. if(Page != null && Enabled && !Checked)
  44. {
  45. Page.RegisterRequiresPostBack(this);
  46. }
  47. if(GroupName.Length == 0)
  48. {
  49. GroupName = UniqueID;
  50. }
  51. }
  52. internal override void RenderInputTag(HtmlTextWriter writer, string id)
  53. {
  54. writer.AddAttribute(HtmlTextWriterAttribute.Id, id);
  55. writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
  56. writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueGroupNamePrivate);
  57. writer.AddAttribute(HtmlTextWriterAttribute.Value, ValueAttributePrivate);
  58. if(Checked)
  59. {
  60. writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
  61. }
  62. if(AutoPostBack)
  63. {
  64. writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.GetPostBackClientEvent(this, ""));
  65. writer.AddAttribute("language", "javascript");
  66. }
  67. if(AccessKey.Length > 0)
  68. {
  69. writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
  70. }
  71. if(TabIndex > 0)
  72. {
  73. writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString(NumberFormatInfo.InvariantInfo));
  74. }
  75. writer.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Input);
  76. writer.RenderEndTag();
  77. }
  78. private string UniqueGroupNamePrivate
  79. {
  80. get
  81. {
  82. string retVal = GroupName;
  83. if(UniqueID.LastIndexOf(":") >= 0)
  84. {
  85. retVal += UniqueID.Substring(UniqueID.LastIndexOf(":") + 1);
  86. }
  87. return retVal;
  88. }
  89. }
  90. private string ValueAttributePrivate
  91. {
  92. get
  93. {
  94. string retVal = Attributes["value"];
  95. if(retVal == null)
  96. {
  97. retVal = ID;
  98. }
  99. if(retVal == null)
  100. {
  101. retVal = UniqueID;
  102. }
  103. return retVal;
  104. }
  105. }
  106. bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
  107. {
  108. if(postCollection[UniqueGroupNamePrivate] != null && postCollection[UniqueGroupNamePrivate] == ValueAttributePrivate)
  109. {
  110. if(!Checked)
  111. {
  112. Checked = true;
  113. }
  114. return true;
  115. }
  116. if(Checked)
  117. {
  118. Checked = false;
  119. }
  120. return true;
  121. }
  122. void IPostBackDataHandler.RaisePostDataChangedEvent()
  123. {
  124. OnCheckedChanged(EventArgs.Empty);
  125. }
  126. }
  127. }