RadioButton.cs 3.0 KB

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