2.0 Dynamically added usercontrol: Cannot "find" control!!

O

OJ

Hi,
I have added code to run on a button click event which adds
usercontrols to an asp:panel control held in a contentplaceholder
defined by the masterpage.

for (int x = 0; x < (Master.PartyCtrl.Adults ); x++)
{
ASP.usercontrols_passenger_ascx passenger = new
ASP.usercontrols_passenger_ascx();
this.pnlAdults.Controls.Add(passenger);
passenger.ID = "passAd" + x.ToString();
passenger.RoomSource = this.SABSRooms1.Rooms;
passenger.BindControls();
}


This works nicely and displays as many or as few of the Passenger
control as required. However, when the passenger has filled in various
bits of info and clicks the submit button again I cannot find these
controls that have been added to retrieve their value. Can anyone help?

O
 
K

Karl Seguin [MVP]

Dynamically added controls aren't magically re-added on postback. Rather,
they need to be recreated in order to have their values re-populated from
viewstate. Normally, one would save information for what needs to be
reloaded in the viewstate...

your for statement..
passenger.BindControl
end for
ViewState.Add("NumberOfPassenbers", Master.PartyCtrl.Adults.Count);

if page.ispostback and Viewstate("NumberOfPassengers") is not nothing then
for (int x = 0; x < (int)ViewState("NumberOfPassengers"); ++i)
//re-create the control and re-add it to the panel
next


that's a good mix of vb.net and c#

As an alternative you can use Denis Bauer's (free)
DynamicControlsPlaceholder:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Karl
 
O

OJ

I had a feeling someone might say something like that....:eek:(

I dont really understand. If the controls are added to the page and the
page is then Posted back, would not the values of the control be
accessible to me after the post back? Do I need to recreate the
controls just to let asp.net find the correct viewstate values for
them?

Also, you describe re-adding the controls to the panel, but how would I
retrieve the viewstate for the server controls held within the user
control? I don't want the controls shown again. I just want to farm the
values from the textboxes and dropdownlists from in these controls.

I do not know how many passengers there are until I need to add
controls to the page cover them. I have to collect their names DoB etc.
I have a passenger ascx which I add to the page up to nine times. The
user adds the detail for each passenger and clicks submit. I add those
values to a booking query and confirmation is returned of who staying
in which hotel. I do not need to see or use the passenger controls
again...

Thanks for your help,

O
 
K

Karl Seguin [MVP]

Comments are inline.

--
http://www.openmymind.net/
http://www.fuelindustries.com/


OJ said:
I had a feeling someone might say something like that....:eek:(

I dont really understand. If the controls are added to the page and the
page is then Posted back, would not the values of the control be
accessible to me after the post back? Do I need to recreate the
controls just to let asp.net find the correct viewstate values for
them?
Yup.

Also, you describe re-adding the controls to the panel, but how would I
retrieve the viewstate for the server controls held within the user
control? I don't want the controls shown again. I just want to farm the
values from the textboxes and dropdownlists from in these controls.

Request.Form might be an option, but that would require that you generate
predictable/static name attributes for your controls, rather than the
autogenerated ones ASP.NET will do for you.
Otherwise, you need to reload the controls, get their values, and turn off
their visibility in the onRender event or something...
I do not know how many passengers there are until I need to add
controls to the page cover them. I have to collect their names DoB etc.
I have a passenger ascx which I add to the page up to nine times. The
user adds the detail for each passenger and clicks submit. I add those
values to a booking query and confirmation is returned of who staying
in which hotel. I do not need to see or use the passenger controls
again...

AS unpure as it sounds, the Request.Form idea has a lot of advantages -
namely ease of use and performance. The difficulty comes in cleanly
generating textboxes wthout reverting to unmaintainable classic-asp
practices.
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top