views:

1129

answers:

4

hi

i have a page that dynamic create a table of contacts, if the contact got an email i also create an image button with a click event. i have a similar function in the rest of the page that works perfectly. and i used this before without any problems.

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 is living insid a java popup window. but i have paging numbers with similar event creation that works fine. but they are Linkbuttons.

A: 

Where are you calling your Create method? You need to do it before the other event handlers run, ideally in the Page.Init. Otherwise, the data posted back to the page are indicated an event firing for a control that doesn't yet exist.

I would also make sure that you give your ImageButton an ID. It will make debugging a lot 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 was clicked there.

Robin Day
A: 

You'll have to create the dynamic controls on EVERY postback. Also check the code in the imgEmail_Click event handler; if you have created the event handler method using .NET IDE's Alt + Shift + F10 method, then there's a chance that you have not removed this line -

throw new Exception("The method or operation is not implemented.");
Kirtan
A: 

If I´m not misstaking the imagebutton is a submit kind of button while the linkbutton is an a-tag with javascript. Maybe changing your imagebutton click (ie usesubmitbehaviour set to false) will solve your problem.

Johan Leino
A: 

Make sure the event handler is added on your postbacks. When adding it just on initial page load, the event won't be handled! (Just encountered and solved this problem myself.)

Protector one