How do I get dynamically added ASP.NET buttons to fire events?
<p>This is probably a simple question but I am not an ASP.NET developer and I am quite stuck.</p>

<p>I have a simple search routine that returns between zero and several hundred results. Each of these must be added to the page as a button and I want to set the text of the button and the CommandArgument property so that when the button is clicked I can read the CommandArgument back and react accordingly.</p>

<p>However, when I click the button the event does not run at all. How can I get it to run?</p>

<p>The code for building the button list (simplified for readability) is as follows:</p>

<pre><code>    foreach (SearchResult sr in searchResults)
    {
        Button result = new Button();
        result.Text = sr.name;
        result.CommandArgument = sr.ID.ToString();
        AccountSearchResults.Controls.Add(result);
        result.Click += new EventHandler(SearchResultClicked);
        AccountSearchResults.Controls.Add(new LiteralControl("&lt;br/&gt;"));
    }
</code></pre>

<p>At the minute to test, I have popped a label on the form to put the CommandArgument in. This code is never executed though.</p>

<pre><code>   void SearchResultClicked(object sender, EventArgs e)
    {
        Label1.Text = ((Button)sender).CommandArgument;
    }
</code></pre>
