4

Button won't call it's event.

called in another button:

placeHolder.Controls.Add(CreateButton());

create button:

public Button CreateButton()
{
    Button btn = new Button();
    btn.ID = "id";
    btn.Text = "some text";
    btn.Attributes.Add("onclick", "return false;");
    btn.Click += new EventHandler(btn_Click);
    return btn;
}

Functionality:

private void btn_Click(object sender, EventArgs e)
{
     // do something.
}

places debug lines to find the source, it's simply not calling btn_Click() when clicked. What's missing?

thatguy
  • 99
  • 1
  • 11
  • 1
    have you tried to just add the method to the eventlist like: `btn.Click += btn_Click;`? – Sebastian L Aug 20 '15 at 12:55
  • You have to add your button on every loading of the page, otherwise ASp.NET what's the source of the event that was fired – Andrei Aug 20 '15 at 12:56
  • Doesn't work Sebastian, Andrei - I don't want the button there unless another button is clicked, so it's only put in the placeholder on another buttons event click. – thatguy Aug 20 '15 at 13:24

1 Answers1

4

This code prevents the click event from firing:

btn.Attributes.Add("onclick", "return false;");

Remove this code, or change it to:

btn.Attributes.Add("onclick", "return true;");

EDIT

I am tried this code and it worked correctly. PlaceHolder is in form tag and runat attribute is server:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        placeHolder.Controls.Add(CreateButton());
}

public Button CreateButton()
{
    Button btn = new Button();
    btn.ID = "id";
    btn.Text = "some text";
    btn.Click += btn_Click;
    return btn;
}

private void btn_Click(object sender, EventArgs e)
{

}
Mitat Koyuncu
  • 928
  • 8
  • 20
  • I put that in to prevent the post back from firing, removing/changing to true still doesn't fire the event: btn_Click – thatguy Aug 20 '15 at 13:21
  • @thatguy see may edited answer. I am tested this with `VS2013`. – Mitat Koyuncu Aug 20 '15 at 13:30
  • Thanks, I changed it to load through Page_Load instead of through a another button, and simply set it's visibility property in the button - Accepted. – thatguy Aug 20 '15 at 13:34
  • How to stop the postback? – thatguy Aug 20 '15 at 13:46
  • Thanks for your help, but that just checks if it is a postback - instead of preventing it on the button. – thatguy Aug 20 '15 at 13:53
  • @thatguy are you want no postback when button clicked? – Mitat Koyuncu Aug 20 '15 at 13:56
  • Yea that's the idea, the btn_Click is called now, but prevention of postback has been removed. – thatguy Aug 20 '15 at 14:02
  • If you want no postback you should write click codes at `javascript` and `return false`. But if you want write click codes at `c#` you should use `UpdatePanel`. If you ask another question about this I will answer with my codes. – Mitat Koyuncu Aug 20 '15 at 14:08
  • See information about UpdatePanel -> [link](http://stackoverflow.com/questions/553073/adding-controls-dynamically-to-an-updatepanel-in-asp-net-ajax) – Mitat Koyuncu Aug 20 '15 at 14:10