dynamic button - does not go to handler

  • Thread starter Thread starter greg
  • Start date Start date
G

greg

Hi

here is what I am doing - building a table dynamically

HtmlTableRow objRow = new HtmlTableRow();

tblStuff.Rows.Add(objRow);

HtmlTableCell objCellMain1 = new HtmlTableCell();

ImageButton btn1 = new ImageButton();

objCellMain1.Controls.Add(btn1);

btn1.Click += new
System.Web.UI.ImageClickEventHandler(this.changeBtn_Click);

btn1.AlternateText = "333";

objRow.Cells.Add(objCellMain1);



and of course I have that method defined



private void changeBtn_Click(object sender,
System.Web.UI.ImageClickEventArgs e)

{

// get id

ImageButton btn = (ImageButton)sender;

String vid = btn.AlternateText;

etc etc.



IT shows on the screen fine

But when I click on the button it does not go into changeBtn_Click

!!!!! HELP

Thanks

GSL
 
greg said:
Hi

here is what I am doing - building a table dynamically

HtmlTableRow objRow = new HtmlTableRow();

tblStuff.Rows.Add(objRow);

HtmlTableCell objCellMain1 = new HtmlTableCell();

ImageButton btn1 = new ImageButton();

objCellMain1.Controls.Add(btn1);

btn1.Click += new
System.Web.UI.ImageClickEventHandler(this.changeBtn_Click);

btn1.AlternateText = "333";

objRow.Cells.Add(objCellMain1);



and of course I have that method defined



private void changeBtn_Click(object sender,
System.Web.UI.ImageClickEventArgs e)

{

// get id

ImageButton btn = (ImageButton)sender;

String vid = btn.AlternateText;

etc etc.



IT shows on the screen fine

But when I click on the button it does not go into changeBtn_Click

!!!!! HELP

Thanks

GSL
Greg,

I suspect you are generating the controls in something like:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
// Create controls
CreateControls();
}
}

Dynamic controls need to be recreated *every* time the page is created
to allow ASP.NET to restore view state, postback data and event wireup
to the dynamically created controls. So remember to recreate them on the
postback as well.

HTH
Kev
 
Because your ImageButton is created dynamically,

ImageButton btn1 = new ImageButton();

you need to recreate the button on a postback in order for the button event to fire.

Tommy,
 
Back
Top