Handling events on automatically generated web controls

  • Thread starter Thread starter JezB
  • Start date Start date
J

JezB

I am dynamically creating WebControls and creating them on my page. Some of
these are buttons, and I attach events to these buttons. The problem is
these force a post-back, so I must regenerate all the webcontrols in the
same way (I am assuming - am I right?).

It was all working fine but now I get the error:

"An error has occurred because a control with auto-generated id '_ctl129'
could not be located to raise a postback event. To avoid this error,
explicitly set the ID property of controls that raise postback events."

occurring between Page_Load and actually firing the event. I can basically
understand why but I dont know why it was working and now it isnt (I just
added some code to evaluate a dataset in generating the webcontrols - if I
take this out it works again).

What can I do about it ?
 
Incidentally, I have tried generating IDs for my buttons as the error
suggests but this doesnt help. Same thing happens.
 
JezB said:
Incidentally, I have tried generating IDs for my buttons as the error
suggests but this doesnt help. Same thing happens.
What dynamic method are you using to create those web controls? Adding
them to a repeater maybe? Loading from a path? When you say
WebControls, I'm assuming you mean the intrinsic controls, such as
asp:textbox or are you maybe talking about Customr Web Controls you
created, or Custome User Controls? Too many pieces missing to give you
a definite answer ... a code sample would help.

ib.
 
I create an HtmlTable object then in a loop populate the rows and columns -
some of the columns contain ImageButton controls, to which I attach the
events. Then I add the HtmlTable to a placeholder that I have embedded into
the design surface. ie (pseudo-code) :-

HtmlTable t = new HtmlTable();
foreach (...)
{
HtmlTableRow rw = new HtmlTableRow();

// add a button column
HtmlTableCell tb1 = new HtmlTableCell();
ImageButton b1 = new ImageButton();
b1.Click += new ImageClickEventHandler(b1_Click);
tb1.Controls.Add(b1);
rw.Cells.Add(tb1);

// add some other columns
...

// add the row to the table
t.Rows.Add(rw);
}
// add the table to the placeholder
PlaceHolder1.Controls.Add(t);

Clicking on the generated imagebutton forces a postback before the event is
called (is there any way to prevent this?) so the same loop is executed
before the event fires. Problem being that the event does NOT then fire, I
get the error.
 
I got round it by saving my HtmlTable object in session, then on postpack
reinstating it and adding it again to the placeholder (rather than going
through the loop again).
 

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

Back
Top