Accessing Dynamically Loaded UserControls

G

Guest

I have a problem with accessing controls that I have loaded dynamically and
added to a web page.

The scenario:
I have a webpage that displays multiple instances of a user control on the
page. The number of controls that are displayed can be adjusted by the user
so the controls are added to the page dynamically using the following code in
the Page_Init event handler:

For i as Integer = 1 to NumberOfControls
Dim ctl as MyCustomControl = CType(LoadControl(“MyCustomControl.ascxâ€),
MyCustomControl)

' set some custom properties on ctl
panControls.Controls.Add(ctl)
Next

This part of the code all works fine and I can load the controls onto the
page without any problems.

The problem:
MyCustomControl contains a save method which will update the database with
the data that is entered onto the control. Rather than having a save button
on the control which fires the update, I have one save button on the page
which is meant to update all controls on the page. I intended to loop
through the Controls collection of panControls when the save button is
clicked and call save on each of the custom controls that are in the
collection. It seems I am unable to do this however. When I check
panControls.Controls.Count it only returns 1 regardless of how many custom
controls I have added (the control it is counting by the way is a literal
control which seems to be put there automatically). I have stepped through
the code that is loading the controls and the count property is being updated
to reflect that I am adding controls, however they aren’t there when the page
is posted back (I’ve also ensured the code to load the controls is only
executed if Page.IsPostBack is false).

Can anybody suggest where im going wrong?
 
G

Guest

Are you giving each control a unique ID propertry before you add it to the
Panel?
Peter
 
G

Guest

Peter,

I have tried giving the controls a unique ID and then trying to pick up the
control using panControls.FindControl("ControlID") but this returns nothing.

Kevin
 
B

bruce barker

you need to recreate the controls on postback so they exist. if its just
a count, store the number in a hidden field (or viewstate), you can look
at on postback.

-- bruce (sqlwork.com)
 
G

Guest

This sounds like it could be the cause of my problem. If I do need to
re-create the controls on postback however, how do I get at the data that has
been entered into the controls so that I can re-create them?

I assumed with me creating a composite control using standard .NET controls
that persistance of data would just come for free without me having to add
any aditional code.

Kevin
 
G

Guest

Ive tested this today and it works fine, this is what I was after.

Thanks for your help!

Kevin
 

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,754
Messages
2,569,520
Members
44,996
Latest member
rainocode

Latest Threads

Top