RadioButton.cs 3.0 KB

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