tags:

views:

93

answers:

3

i have a dinamically created table and in some of the cells i have an image button associated with the redBall_Click() handler

(here is the code behind)

    TableCell cellOK = new TableCell();
 cellOK.Style.Add(HtmlTextWriterStyle.TextAlign, "Center");
 cellOK.Width = new Unit("3%");
 ImageButton redBall = new ImageButton();
 redBall.CausesValidation = false;
 redBall.ID = id;
 redBall.ImageUrl = "~/App_Themes/DotRed.png";

    redBall.Click += new ImageClickEventHandler(redBall_Click);
 cellOK.Controls.Add(redBall);

my problem is that the redBall_Click() method is never called (neither after the PostBack)

how can i solve this?

p.s. i can't use a static link because every ImageButton is associated with a specific ID that i must pass to the page i call (for example as a Session object)

A: 

Have you tried putting a breakpoint in the page load method before clicking the image? I suspect this might show that the postback is happening, even if the image click handler isn't getting fired.

Is the code to create the image button also invoked during a postback? If not, the page will postback, but the image button won't exist to invoke the click handler. Sounds bizarre, but it's caught me out a few times.

Simes
+1  A: 

You have to set up the event handler within OnInt and do it every time the page is created, even during a postback. Make sure you don't have the code within a block like:

if(!this.IsPostback)
{
  ...
}
Damien McGivern
A: 

I finally resolved (just yesterday).

I guess I was building my buttons too late: in the Page_PreRender (after the event was to be fired) maybe if i put the creation in Page_Load (yes, I create everything also on PostBack) it would handle the click, but I think I won't try since I found a workaround.

I'll explain for anyone who could have the same problem:

private TableCell cellOK(string id)
{
 TableCell cellOK = new TableCell();
 cellOK.Style.Add(HtmlTextWriterStyle.TextAlign, "Center");
 Image redBall = new Image();
 redBall.ID = id; // useless
 redBall.ImageUrl = "redball.gif";
 HyperLink hl = new HyperLink();
 hl.Controls.Add(redBall);
 hl.NavigateUrl = MyPage + "?id=" + id;
 cellOK.Controls.Add(hl);
 return cellOK;
}

and in Page_Init()

string m_QueryStringId = Request.QueryString.Get("id");
if (!string.IsNullOrEmpty(m_QueryStringId))
{
 // Do something
}