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 article 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);


Now if you press the button which you generate at runtime this event will be fired.

Now move to the next part. If you see my previous article then you can see that I also created some more controls. Suppose I want to access these controls on that button control.

Just remember the normal process if you have to access the button you will directly get value by using control with their ID. But here we have to find the control and cast them to the control.


		protected void btn_Click(object sender, EventArgs e)

        int Fnum = Convert.ToInt32(((TextBox)Panel1.FindControl("Fnum")).Text);
        int snum = Convert.ToInt32(((TextBox)Panel1.FindControl("Snum")).Text);
        int res = Fnum + snum;



Email Address

For any query you can send mail at