RadioButton.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // System.Web.UI.WebControls.RadioButton.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) Gaurav Vaish (2002)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Globalization;
  35. using System.Web;
  36. using System.Web.UI;
  37. using System.ComponentModel;
  38. using System.ComponentModel.Design;
  39. namespace System.Web.UI.WebControls
  40. {
  41. [Designer ("System.Web.UI.Design.WebControls.CheckBoxDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  42. public class RadioButton : CheckBox, IPostBackDataHandler
  43. {
  44. public RadioButton () : base ()
  45. {
  46. }
  47. [DefaultValue (""), WebCategory ("Behavior")]
  48. [WebSysDescription ("The name of the group that this control belongs to.")]
  49. public virtual string GroupName
  50. {
  51. get {
  52. object o = ViewState ["GroupName"];
  53. return (o == null) ? String.Empty : (string) o;
  54. }
  55. set { ViewState ["GroupName"] = value; }
  56. }
  57. protected override void OnPreRender (EventArgs e)
  58. {
  59. base.OnPreRender (e);
  60. if (Page != null && Enabled && !Checked)
  61. Page.RegisterRequiresPostBack (this);
  62. if(GroupName.Length == 0)
  63. GroupName = UniqueID;
  64. }
  65. internal override void RenderInputTag (HtmlTextWriter writer, string id)
  66. {
  67. writer.AddAttribute (HtmlTextWriterAttribute.Id, id);
  68. writer.AddAttribute (HtmlTextWriterAttribute.Type, "radio");
  69. writer.AddAttribute (HtmlTextWriterAttribute.Name, UniqueGroupNamePrivate);
  70. writer.AddAttribute (HtmlTextWriterAttribute.Value, ValueAttributePrivate);
  71. if (Checked)
  72. writer.AddAttribute (HtmlTextWriterAttribute.Checked, "checked");
  73. if (!Enabled)
  74. writer.AddAttribute (HtmlTextWriterAttribute.Disabled, "disabled");
  75. if (AutoPostBack){
  76. writer.AddAttribute (HtmlTextWriterAttribute.Onclick,
  77. Page.GetPostBackClientEvent (this, ""));
  78. writer.AddAttribute ("language", "javascript");
  79. }
  80. if (AccessKey.Length > 0)
  81. writer.AddAttribute (HtmlTextWriterAttribute.Accesskey, AccessKey);
  82. if (TabIndex != 0)
  83. writer.AddAttribute (HtmlTextWriterAttribute.Tabindex,
  84. TabIndex.ToString (NumberFormatInfo.InvariantInfo));
  85. writer.RenderBeginTag (System.Web.UI.HtmlTextWriterTag.Input);
  86. writer.RenderEndTag ();
  87. }
  88. private string UniqueGroupNamePrivate
  89. {
  90. get {
  91. string retVal = GroupName;
  92. int unique = UniqueID.LastIndexOf (':');
  93. if (unique >= 0)
  94. retVal += UniqueID.Substring (unique + 1);
  95. return retVal;
  96. }
  97. }
  98. private string ValueAttributePrivate
  99. {
  100. get {
  101. string retVal = Attributes ["value"];
  102. if (retVal != null)
  103. return retVal;
  104. if (ID != null)
  105. return ID;
  106. return UniqueID;
  107. }
  108. }
  109. bool IPostBackDataHandler.LoadPostData (string postDataKey,
  110. NameValueCollection postCollection)
  111. {
  112. bool _checked = Checked;
  113. if (postCollection [UniqueGroupNamePrivate] == ValueAttributePrivate){
  114. if (_checked)
  115. return false;
  116. Checked = true;
  117. return true;
  118. }
  119. if (_checked)
  120. Checked = false;
  121. return true;
  122. }
  123. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  124. {
  125. OnCheckedChanged (EventArgs.Empty);
  126. }
  127. }
  128. }