dynamic button - does not go to handler

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
 
K

Kevin Kenny

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
 
T

Tommy

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top