R – Dynamic button control in AJAX

ajaxcontrolsdynamic

I create a button control and it shows up, but when clicked event does not fire, not sure if it could be because control gets greated after previous event in AJAX which fires fine.

Should only get executed once.

Any ideas would be appreciated, thanks.

        Button btnCommentSave = new Button();
        btnCommentSave.ID = "mySavebtnComments" ;
        btnCommentSave.Text = "Publish";
        btnCommentSave.BackColor = Color.Aquamarine;
        phBlog.Controls.Add(btnCommentSave);
        btnCommentSave.CommandArgument = row["ID"].ToString();
        btnCommentSave.Click  += new EventHandler(btnSave_Click);

protected void btnSave_Click(object sender, EventArgs e)
{
other code
}

Best Answer

You are not by any chance executing this code on Page_Load? You have to make sure that this code is executed only once, otherwise the event won't fire.