【IT168技术文档】
先重写RadionButton:
1 [ToolboxData("<{0}:GroupRadioButton runat=server></{0}:GroupRadioButton>")] 2 public class GroupRadioButton : RadioButton, IPostBackDataHandler 3 { 4 public GroupRadioButton() 5 : base() 6 { 7 } 8 9 Properties#region Properties 10 11 private string Value 12 { 13 get 14 { 15 string val = Attributes["value"]; 16 if (val == null) 17 val = UniqueID; 18 else 19 val = UniqueID + "_" + val; 20 return val; 21 } 22 } 23 24 #endregion 25 26 Rendering#region Rendering 27 28 protected override void Render(HtmlTextWriter output) 29 { 30 RenderInputTag(output); 31 } 32 33 private void RenderInputTag(HtmlTextWriter htw) 34 { 35 htw.AddAttribute(HtmlTextWriterAttribute.Id, ClientID); 36 htw.AddAttribute(HtmlTextWriterAttribute.Type, "radio"); 37 htw.AddAttribute(HtmlTextWriterAttribute.Name, GroupName); 38 htw.AddAttribute(HtmlTextWriterAttribute.Value, Value); 39 if (Checked) 40 htw.AddAttribute(HtmlTextWriterAttribute.Checked, "checked"); 41 if (!Enabled) 42 htw.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled"); 43 44 string onClick = Attributes["onclick"]; 45 if (AutoPostBack) 46 { 47 if (onClick != null) 48 onClick = String.Empty; 49 onClick += Page.GetPostBackClientEvent(this, String.Empty); 50 htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick); 51 htw.AddAttribute("language", "javascript"); 52 } 53 else 54 { 55 if (onClick != null) 56 htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick); 57 } 58 59 if (AccessKey.Length > 0) 60 htw.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey); 61 if (TabIndex != 0) 62 htw.AddAttribute(HtmlTextWriterAttribute.Tabindex, 63 TabIndex.ToString(NumberFormatInfo.InvariantInfo)); 64 htw.RenderBeginTag(HtmlTextWriterTag.Input); 65 htw.RenderEndTag(); 66 } 67 68 #endregion 69 70 IPostBackDataHandler Members#region IPostBackDataHandler Members 71 72 void IPostBackDataHandler.RaisePostDataChangedEvent() 73 { 74 OnCheckedChanged(EventArgs.Empty); 75 } 76 77 bool IPostBackDataHandler.LoadPostData(string postDataKey, 78 System.Collections.Specialized.NameValueCollection postCollection) 79 { 80 bool result = false; 81 string value = postCollection[GroupName]; 82 if ((value != null) && (value == Value)) 83 { 84 if (!Checked) 85 { 86 Checked = true; 87 result = true; 88 } 89 } 90 else 91 { 92 if (Checked) 93 Checked = false; 94 } 95 return result; 96 } 97 98 #endregion 99 }