Dynamically generated Button click not getting called?
<p>I have associated a Button ID while generating Buttons dynamically and created a Button event handler as follows:</p>

<pre><code>btn.Click += new EventHandler(btn_Click);
</code></pre>

<p>Button Click event:</p>

<pre><code>protected void btn_Click(object sender,EventArgs e)
{
    Button b = new Button();
    b = (Button)sender;
    string i = b.ID.Substring(b.ID.Length - 1, 1);
    int j1 =Convert.ToInt32(i);
    id1 = to[j1];
    Page.ClientScript.RegisterClientScriptBlock(Type.GetType("System.String"), 
        "addScript", "PassValues('" + id1 + "')", true);
    ScriptManager.RegisterStartupScript(this, this.GetType(), 
       "sendMessage", "javascript:sendMessage(); ", true);
}
</code></pre>

<p>However this event is not getting called i have placed all my controls inside an UpdatePanel</p>

<p>EDIT: What is happening is on button click somehow a function is getting called which is present on <code>Page.Load</code></p>

<pre><code>protected void getEntriesRight()
{
    j = (int)Session["j"];

    int n1 = j + 3;

    for (; j &lt;= n1; j++)
    {
        if (j &lt; fr.data.Length)
        {
            HtmlGenericControl listItem = new HtmlGenericControl("li");
            HtmlGenericControl a1 = new HtmlGenericControl("a");
            Label anchor = new Label();
            Image im = new Image();

            btn = new Button();
            im.ImageUrl = fr.data[j].pic_square;
            im.Height = 45;
            im.Width = 47;


            btn.CssClass = "btn-add";
            btn.Text = "Invite";
            to[j] = fr.data[j].uid;

            btn.ID = "btn" + j;


            a1.Attributes.Add("href", "#");
            anchor.Text = fr.data[j].name;
            a1.Controls.Add(btn);
            a1.Controls.Add(im);
            a1.Controls.Add(anchor);
            listItem.Controls.Add(a1); 
            list.Controls.Add(listItem);

                btn.Click += new EventHandler(btn_Click);

        }
    }

    Session["j"] = j;
}
</code></pre>

<p>Help!</p>
