If you want to add event at run time like click event in page load method. For this types of problem you can use EventHandler class. Create a object of this class also pass new method as a parameter.
<asp:Button ID="B1" runat="server" CommandName="Add" Text="Button" />
</div>
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
B1.Click += new System.EventHandler(this.calculate);
}
protected void calculate(object sender, System.EventArgs e)
{
switch (((Button)sender).CommandName)
{
case "Add": Response.Write("hello");
break;
}
}
}
Code Generate the following output
Source Code
<div><asp:Button ID="B1" runat="server" CommandName="Add" Text="Button" />
</div>
Business Code
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
B1.Click += new System.EventHandler(this.calculate);
}
protected void calculate(object sender, System.EventArgs e)
{
switch (((Button)sender).CommandName)
{
case "Add": Response.Write("hello");
break;
}
}
}
Code Generate the following output