September 15, 2009 by Christoff Truter C# ASP.NET
Creating custom controls that expose events can potentially prove to be quite tedious if we're presented with examples like the following:
Notice the following code, from the example you'll find on the MSDN links above:public class MyButton: Control, IPostBackEventHandler { public event EventHandler Click; protected virtual void OnClick(EventArgs e) { if (Click != null) { Click(this, e); } } public void RaisePostBackEvent(string eventArgument){ OnClick(new EventArgs()); } protected override void Render(HtmlTextWriter output) { output.Write("<INPUT TYPE = submit name = " + this.UniqueID + " Value = 'Click Me' />"); } }
HtmlInputSubmit _control = new HtmlInputSubmit() { Name = this.UniqueID }; _control.RenderControl(output);
public class SearchBox : CompositeControl { public event EventHandler Click; TextBox _TextBox = new TextBox(); Button _Button = new Button() { CommandName = "Click", Text = "Search" }; [Browsable(true)] [Category("Appearance")] public string Text { get { return _TextBox.Text; } set { _TextBox.Text = value; } } protected virtual void OnClick(EventArgs e) { if (Click != null) { Click(this, e); } } protected override bool OnBubbleEvent(object source, EventArgs args) { CommandEventArgs _CommandEventArgs = args as CommandEventArgs; if (_CommandEventArgs != null) { if (_CommandEventArgs.CommandName == "Click") { OnClick(args); return true; } } return false; } protected override void CreateChildControls() { this.Controls.Add(_TextBox); this.Controls.Add(_Button); base.CreateChildControls(); } }
MSDN link - ToolboxData October 26, 2016 by Christoff Truter
Have a look over here -> https://msdn.microsoft.com/en-us/library/ms366537.aspx