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?
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?