47

I am creating one button on a page dynamically. Now I want to use the button click event on that button.

How can I do this in C# ASP.NET?

6 Answers 6

63
Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);

//protected void button_Click (object sender, EventArgs e) { }
Sign up to request clarification or add additional context in comments.

7 Comments

You have to create button in OnInit method, otherwise event handler won't work
First of all thnx IN this u write button.Click += (s,e) => { your code; }; s= object sender and e = event argument ri8? but then also button click event not fire ... can u explain me that how it can work..
@amitvyas: This is shorter, but more complex version of the same code. Instead of explicit event handler declaration - implicit using lambda expression and anonymous method: { this is anon method with 2 arguments declared }
This also didn't work for me; the event handler never fired. It might be because a new event handler is created for the button each time a request is received.
dynamic generated event must be register in Page_Load/Page_Init event outside of !IsPostBack.
|
43

The easier one for newbies:

Button button = new Button();
button.Click += new EventHandler(button_Click);

protected void button_Click (object sender, EventArgs e)
{
    Button button = sender as Button;
    // identify which button was clicked and perform necessary actions
}

4 Comments

What is the non-newbie version?
@MichaelMello: Non-newbie one could have been the lambda one. "button.click += (sender, e) => { // do something here }" :)
Can I pass other arguments along with this? ie button.Click += new EventHandler(button_Click("Test"));?
Nevermind, this worked: button.Click += (se, ev) => button_Click(se, ev, qo);
13

Simply add the eventhandler to the button when creating it.

 button.Click += new EventHandler(this.button_Click);

void button_Click(object sender, System.EventArgs e)
{
//your stuff...
}

Comments

11

It is much easier to do:

Button button = new Button();
button.Click += delegate
{
   // Your code
};

Comments

2

You can create button in a simple way, such as:

Button button = new Button();
button.Click += new EventHandler(button_Click);

protected void button_Click (object sender, EventArgs e)
{
    Button button = sender as Button;
    // identify which button was clicked and perform necessary actions
}

But event probably will not fire, because the element/elements must be recreated at every postback or you will lose the event handler.

I tried this solution that verify that ViewState is already Generated and recreate elements at every postback,

for example, imagine you create your button on an event click:

    protected void Button_Click(object sender, EventArgs e)
    {
       if (Convert.ToString(ViewState["Generated"]) != "true")
        {
            CreateDynamicElements();
        }
    
    }

on postback, for example on page load, you should do this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToString(ViewState["Generated"]) == "true") {
            CreateDynamicElements();
        }
    }

In CreateDynamicElements() you can put all the elements you need, such as your button.

This worked very well for me.

public void CreateDynamicElements(){

    Button button = new Button();
    button.Click += new EventHandler(button_Click);

}

Comments

0

Let's say you have 25 objects and want one process to handle any one objects click event. You could write 25 delegates or use a loop to handle the click event.

public form1()
{
    foreach (Panel pl  in Container.Components)
    {
        pl.Click += Panel_Click;
    }
}

private void Panel_Click(object sender, EventArgs e)
{
    // Process the panel clicks here
    int index = Panels.FindIndex(a => a == sender);
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.