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



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.

For Example:-

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;
        Response.Write(res);

   
    }

Hope you enjoyed the article.

In my next article I will show how we can use single event with multiple controls.

4 comments:

  1. Its help me a alot to understand this concept...

    ReplyDelete
  2. Nice blog! Very interesting and informative post….Thank's for sharing such a nice information….Keep it up!!

    Web Design Services in Gravesend
    Web Development in Gravesend
    PHP Services in Gravesend

    ReplyDelete
  3. Web Designing and Advance UI Training in noida with HTML5, CSS3, Javascript, Jquery, Bootstrap, UI Testing and Live Project.
    http://www.techaltum.com/webdesigningtraininginnoida.html

    ReplyDelete
  4. Web Designing and Advance UI Training in noida with HTML5, CSS3, Javascript, Jquery, Bootstrap, UI Testing and Live Project.
    http://www.techaltum.com/webdesigningtraininginnoida.html

    ReplyDelete