using System;using System.Web.UI;using System.Web.UI.WebControls;using System.ComponentModel; using System.Text; namespace Controls{ ////// MyTextBox2 的摘要说明。 /// [ToolboxData("<{0}:MyTextBox2 runat=server> ")] public class MyTextBox2 : System.Web.UI.WebControls.WebControl,INamingContainer { private Label lbl; private TextBox txt; protected override void CreateChildControls() { base.CreateChildControls (); lbl = new Label(); lbl.ID = "lbl"; lbl.Style.Add("display", "none"); Controls.Add(lbl); txt = new TextBox(); txt.ID = "txt"; txt.AutoPostBack = true; txt.TextChanged += new EventHandler(txt_TextChanged); Controls.Add(txt); lbl.Attributes.Add("onclick", "showbox(this,'" + txt.ClientID + "');"); ChildControlsCreated = true; } protected override void OnPreRender(EventArgs e) { base.OnPreRender (e); if(txt.Text.Trim().Length == 0) return; double t = 0; try { t = double.Parse(txt.Text.Trim()); } catch(Exception ex) { ex.ToString(); } lbl.Text = t.ToString("c"); } protected override void RenderChildren(HtmlTextWriter writer) { StringBuilder sb = new StringBuilder(); sb.Append(""); this.Page.RegisterStartupScript("MyTextBox2JS", sb.ToString()); lbl.RenderControl(writer); txt.RenderControl(writer); } protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } } private void txt_TextChanged(object sender, EventArgs e) { if(txt.Text.Trim().Length == 0) return; double t = 0; try { t = double.Parse(txt.Text.Trim()); } catch(Exception ex) { ex.ToString(); } lbl.Style.Add("display", "block"); lbl.Text = t.ToString("c"); txt.Style.Add("display", "none"); } }}