Event handling of dynamically loaded user control

  • Thread starter Thread starter JB McMichael
  • Start date Start date
J

JB McMichael

I have an aspx page that based on a query loads a user control dynamically.

The aspx page is simple, just a place holder, the code behind queries a
table that stores the names of the ascx files. I then add the control
to the placeholder:

Dim myUC As Control = Page.LoadControl("includes/" & ascx_page)
myplaceholder.Controls.Add(myUC)

Each of these user controls has 2 or 3 textboxes and a Submit button.
Each user control has a onclick method for the Submit button in which
the values of the text boxes are entered in to a database. My problem
is that the event never fires. The code compiles, and the forms load up
just fine, but clicking on the Submit button does nothing. So now I am
stuck and looking for some help.
 
The timing is very important when adding controls dynamically.
Typically you should add your controls in the Init method of the page

If you do it later (e.g. on PreRender), your viewstate gets corrupted.
Corrupted viewstate is the #1 reason for events not firing.
 
Stefan said:
The timing is very important when adding controls dynamically.
Typically you should add your controls in the Init method of the page

If you do it later (e.g. on PreRender), your viewstate gets corrupted.
Corrupted viewstate is the #1 reason for events not firing.

I am adding my controls in the page load. Is there a way to prevent the
viewstate from getting corrupted?
 

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