RadioButton.cs 3.1 KB

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