How to call event in page load method and example of EventHandler class

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.

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
How to call event in page load method and example of EventHandler class

Comments

Popular posts from this blog

difference between structure and union in C Language

Difference between Linear search and Binary Search in c language

Difference between static and dynamic websites