using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Globalization; namespace HuizhongLibrary.Control { [DefaultProperty("Value"), ToolboxData("<{0}:DataGridRadioButton runat=server>"),ParseChildren(true), PersistChildren(false)] public class DataGridRadioButton : RadioButton,INamingContainer,IPostBackDataHandler { public DataGridRadioButton() : base() { this.PreRender +=new EventHandler(DataGridRadioButton_PreRender); } private void DataGridRadioButton_PreRender(object sender, EventArgs e) { // Page.RegisterHiddenField("RadioButton_Checked", this.Checked.ToString()); // Page.RegisterStartupScript("jsSetRadioButton_Checked", ""); // this.Attributes.Add("onclick","document.getElementById('RadioButton_Checked').value = this.Checked;"); // 将控件注册为要求在页回发至服务器时进行回发处理的控件 if (Page != null) Page.RegisterRequiresPostBack(this); } #region Properties private string Value { get { string val = Attributes["value"]; if(val == null) val = UniqueID; else val = UniqueID + "_" + val; return val; } } #endregion #region Rendering protected override void Render(HtmlTextWriter output) { RenderInputTag(output); } private void RenderInputTag(HtmlTextWriter htw) { htw.AddAttribute(HtmlTextWriterAttribute.Id, ClientID); htw.AddAttribute(HtmlTextWriterAttribute.Type, "radio"); htw.AddAttribute(HtmlTextWriterAttribute.Name, GroupName); htw.AddAttribute(HtmlTextWriterAttribute.Value, Value); if(Checked) htw.AddAttribute(HtmlTextWriterAttribute.Checked, "checked"); if(!Enabled) htw.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled"); string onClick = Attributes["onclick"]; if(AutoPostBack) { if(onClick != null) onClick = String.Empty; onClick += Page.GetPostBackClientEvent(this, String.Empty); htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick); htw.AddAttribute("language", "javascript"); } else { if(onClick != null) htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick); } if(AccessKey.Length > 0) htw.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey); if(TabIndex != 0) htw.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString(NumberFormatInfo.InvariantInfo)); htw.RenderBeginTag(HtmlTextWriterTag.Input); htw.RenderEndTag(); } #endregion #region IPostBackDataHandler Members void IPostBackDataHandler.RaisePostDataChangedEvent() { OnCheckedChanged(EventArgs.Empty); } bool IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection) { bool result = false; string value = postCollection[GroupName]; if((value != null) && (value == Value)) { if(!Checked) { Checked = true; result = true; } } else { if(Checked) Checked = false; } // if (postCollection["RadioButton_Checked"]!=null)this.Checked=Convert.ToBoolean(postCollection["RadioButton_Checked"]); return result; } #endregion } }