Web Control Problem
-
I am trying to make a simple control with a button and a label. When you click the button the text of the label should change from "A" to "B" but it doesn’t work. Can anybody tell me what am I doing wrong? using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace WebControlLibrary1 { [DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server>")] public class WebCustomControl1 : System.Web.UI.WebControls.WebControl { private Button b; private Label l; public WebCustomControl1() { b=new Button(); b.Text="Click me!"; b.Click+=new EventHandler(this.b_Click); l=new Label(); l.Text="A"; } private void b_Click(object sender, System.EventArgs e) { l.Text="B"; } protected override void Render(HtmlTextWriter output) { b.RenderControl(output); l.RenderControl(output); } } }
-
I am trying to make a simple control with a button and a label. When you click the button the text of the label should change from "A" to "B" but it doesn’t work. Can anybody tell me what am I doing wrong? using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace WebControlLibrary1 { [DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server>")] public class WebCustomControl1 : System.Web.UI.WebControls.WebControl { private Button b; private Label l; public WebCustomControl1() { b=new Button(); b.Text="Click me!"; b.Click+=new EventHandler(this.b_Click); l=new Label(); l.Text="A"; } private void b_Click(object sender, System.EventArgs e) { l.Text="B"; } protected override void Render(HtmlTextWriter output) { b.RenderControl(output); l.RenderControl(output); } } }
Here enjoy...
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace WebControlLibrary1 { /// /// Summary description for WebCustomControl1. /// [DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server>")] public class WebCustomControl1 : System.Web.UI.WebControls.WebControl, IPostBackEventHandler, INamingContainer { private Label l; private Button b; /// /// Render this control to the output parameter specified. /// /// The HTML writer to write out to public WebCustomControl1() { b = new Button(); b.Text = "button"; b.Click += new EventHandler(this._Handler); b.Width = 100; l = new Label(); l.Text = "A"; } protected override void CreateChildControls() { Controls.Add(b); Controls.Add(l); } private void _Handler(System.Object sender, System.EventArgs e) { l.Text = "B"; } public void RaisePostBackEvent(string Args) { // This function is supposed to handle post back events. } } }
--------------------- A gasp of breath, A sudden death: The tale begun. A rustled page Passes an age: The tale is done. -
Here enjoy...
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace WebControlLibrary1 { /// /// Summary description for WebCustomControl1. /// [DefaultProperty("Text"), ToolboxData("<{0}:WebCustomControl1 runat=server>")] public class WebCustomControl1 : System.Web.UI.WebControls.WebControl, IPostBackEventHandler, INamingContainer { private Label l; private Button b; /// /// Render this control to the output parameter specified. /// /// The HTML writer to write out to public WebCustomControl1() { b = new Button(); b.Text = "button"; b.Click += new EventHandler(this._Handler); b.Width = 100; l = new Label(); l.Text = "A"; } protected override void CreateChildControls() { Controls.Add(b); Controls.Add(l); } private void _Handler(System.Object sender, System.EventArgs e) { l.Text = "B"; } public void RaisePostBackEvent(string Args) { // This function is supposed to handle post back events. } } }
--------------------- A gasp of breath, A sudden death: The tale begun. A rustled page Passes an age: The tale is done.