Showing posts with label Add Click Event in Button at Runtime. Show all posts
Showing posts with label Add Click Event in Button at Runtime. Show all posts

Friday, 29 March 2013

Add Click Event in Button at Runtime


For Asp.net Training  Click Here

Written By:-Isha Malhotra
Email:-malhotra.isha3388@gmail.com


Add Click Event in Button At Runtime

In my last article I discussed how to create control at runtime. Now I am discussing that if we want to make any event on any control then how we generate. To understand the concept of event you must have the knowledge of Delegate

For Example:-

In my Last Artical I create simple program of two number sum where I created control at run time. I am continuing with same example in which now I am creating event for button click and performing sum operation on that controls which we generated at runtime.

In this article I also explain how to access the runtime generated control.

To add event in the button I have to use the delegate EventHandler and add it in the click event and pass the reference of method to the EventHandler which will fire when we click on the button.

Create following method (you can say it event for button click)

protected void btn_Click(object sender, EventArgs e)
    {
   

   
    }

Now we have to pass the reference of this method to that button which we create at runtime. We have to add this reference to the event handler.

  Button btn_Sub = new Button();
        btn_Sub.ID = "bSum";
        btn_Sub.Text = "Sum";
        btn_Sub.Click += new EventHandler(btn_Click);