ASP.NET Events and dynamic controls

K

Kenton Smeltzer

Hello All,

I am having a problem with events and the addition of
controls on a page I am developing. First let me tell you
what I have tried and then maybe someone can see something
I missed.

First I tried adding the controls and the event handlers
for my control in the Initialize Components method of my
page in the hopes that it would fire the event before the
Page_Load method was called but to no avail. It seems that
ASP.NET will not let you handle events before the page is
loaded which is expected but it was worth a shot.

Second I decided to go ahead and add my controls and the
event handlers in the Page_Load method and then add the
new controls as the event fired and then push everything
into the control collection at the OnPreRender this worked
as expected and the events fired before the PreRender the
only problem is that any new control added at PreRender
does not maintain its state. Textboxes loose there text
and any handlers attached to the control loose reference
to the delegate they where pointing to. Anyway I need to
find some way to add controls to the control collection
dynamically and get them down to the browser. Right now I
have a real nasty method that just clears all child
controls and calls the Page_Load again and then when
Page_Load creates the control it calls a delegate to give
the control that launched it a handle to it. But there has
got to be a better way.

How can I get these controls added before the page is
render and get them to keep their state information.

Thanks,
Kenton
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top