Hi!
I'm a little confused now over the following scenario.
I have a webpage with a few buttons and fields and several UserControls
dynamically created from database. All the controls are crated using
the same "template" but are containing different text and so on
depending on the database. All controls contain two buttons.
My first intuition was to create the user controls as early as
possible, in Page_Load or earlier. This worked fine until I implemented
the functions of my buttons (both on the parent page and user
controls). The buttons manipulate the content of the database and those
changes should be reflected by the user controls but they don't.
Nothing strange about that I think because the code of the button
events execute after Page_Load. Now I created a method that creates the
dynamic controls. I cal the method from the Page_Load and from the
event handlers, but that's not working good because then I get two
sets of controls when I click a button. Perhaps nothing strange about
that but the strange thing is, and what I don't understand, is that
when I cal the method from the user controls event handler, no controls
are created. The method is called, I verified that, but It don't create
any controls. Perhaps it had something to do with that control can't
cal method to create it self. OK now I tried to clear the panel / place
holder I put the controls on, to get rid of the double controls. In the
browser it looks ok and I only see one set of controls. But something
is wrong because I have to click twice on the buttons on the control to
make them react correctly. It seams to be the "doPostBack" that, by
some reason, isn't correct. When the page load and e.g. 4 user controls
are created all with buttons "edit" and "delete" the "doPostBack" for
the first control has '_ctl1$...' the next has '_ctl2$...' etc (notice
the numbers 1, 2.. etc). When I click the edit button on the first
control the second control reacts. by displaying the "edit" box. But
now, when I look at the "doPostBack" for the buttons the first control
has '_ctl0$...' the second has '_ctl1$...' etc (the numbers start from
0) and now the buttons work for correct control. Apparently clearing
the panel to create new controls on it isn't working. I see the
correct controls but an "old" control is still there somewhere and
the new control gets wrong event handler associated.
But... now the big question is. What is the correct way of doing this.
Create user controls based on database and with buttons that can modify
the database??????
Thanks!!!
Björn