How to create a custom Event Handler class and predefine the methods to reuse it. This helps to reuse it.
- How to create a button dynamically at runtime
- How to add buttons to a form element.
- How to create a class for a custom event handler.
- How to create a method for an event handler.
- How to assign the custom event handler to a button.
Step 1: Used Namespaces:
using System;
using System.Web.UI.WebControls;
Step 2: Creating a Button at runtime and add to a Form element:
this.form1.Controls.Add(AddButton("Save", "save"));
this.form1.Controls.Add(AddButton("Delete", "delete"));
Step 3: Helps to create a Button at runtime and assign the event to it:
public static Button AddButton(string text, string eventName)
{
EventClass eventClass = new EventClass();
Button but = new Button();
but.Text = text;
but.ID = text + "Button";
but.Click += eventClass.AddEventHandler(eventName);
return but;
}
Step 4: Creating a method for the event handler:


Join the conversation! Your thoughts help the community grow.