A sudden dynamic generated clickbutton click event does not fire.
I have a page that dynamically creates a contact table, if a contact received an email, I also create an image button with a click event. I have a similar function on the rest of the page that works great. And I used this with no problem:
protected void CreateContactsList(IQueryable<AA_BranschFinder.Login.vyWebKontaktpersoner> lContacts) // Creates a table in the aspx from an IQueryable List
{
if (1 == 1)
{
htmlTblContactsContent.Rows.Clear();
foreach (var p in lContacts)
{
HtmlTableRow tr = new HtmlTableRow();
HtmlTableCell tdName = new HtmlTableCell();
HtmlTableCell tdCompanyName = new HtmlTableCell();
HtmlTableCell tdEmailAdress = new HtmlTableCell();
tdName.InnerHtml = p.strFnamn + " " + p.strEnamn;
tdCompanyName.InnerHtml = p.strNamn;
//Displays an image if the contacts has an email
if (p.strEpost != null)
{
ImageButton imgEmail = new ImageButton();
imgEmail.CommandArgument = p.intKundID.ToString();
imgEmail.ImageUrl = "images/symbol_letter.gif";
imgEmail.CssClass = "letter";
imgEmail.Click +=new ImageClickEventHandler(imgEmail_Click);
tdEmailAdress.Controls.Add(imgEmail);
}
tr.Cells.Add(tdCompanyName);
tr.Cells.Add(tdEmailAdress);
tr.Cells.Add(tdName);
htmlTblContactsContent.Rows.Add(tr);
}
}
}
void imgEmail_Click(object sender, ImageClickEventArgs e)
{
Breakpoint here throw new NotImplementedException (); }
The page lives inside a java popup. But I have paging numbers with similar event generation that works great. But these are Linkbuttons.
Where are you calling your Create method? You need to do this before any other event handlers are executed, ideally in Page.Init. Otherwise, the data posted back to the page will be shown for an event that doesn't exist yet.
I also made sure you give your ImageButton ID. This will make debugging easier.
imgEmail.ID = String.Format("EmailImageButton_{0}", p.intKundID);
An alternative solution is to look at the __eventtarget and __eventargument parameters in the Request object and see which button is clicked.
a source to share
You need to create dynamic controls for EVERY postback. Also check the code in the imgEmail_Click event handler; if you created an event handler method using the .NET IDE method Alt + Shift + F10, then chances are that you have not removed this line -
throw new Exception("The method or operation is not implemented.");
a source to share