RadioButton.cs 3.0 KB

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