views:

16

answers:

1

Hi there, Can someone point me in the right direction of adding usercontrols to an update panel.

Basically I have a usercontrol that is setup to capture a persons education details. I want to have a button inside the update that when the user clicks loads the usercontrol below the previous usercontrol.

Any help would be great.

I have got as far as

  UserControl userControl = (UserControl)this.LoadControl(controlName);
  this.Panel1.Controls.Add(userControl);

however I am not sure how to keep the orginal usercontrol and not overwrite it with the newly loaded usercontrol.

A: 

Since you're creating a UserControl dinamically, you must pay attention in how to treat this kind of situation.

Look at this question in StackOverflow:

Accessing controls created dynamically (c#)

Leniel Macaferi
Thanks Leniel for your reply. Ok so just so I am on the right page. In my button event do I save the usercontrol as an object in the pages viewstate then recreate the usercontrol within Page_OnInit event?
It's a possibility. The thing is that you must recreate the dynamic controls after each postback. I just save the data I used to create the controls in Session and then I recreate the controls with the data I recover from Session. See what I described in the answer provided in the above link I shared with you.
Leniel Macaferi
Sorry if this is really basic stuff but I am a little confused. What do I save to session the usercontrol or the textboxes values into session?
You can save the created controls in the Session object. I advise you to implement a method that creates the dynamic controls and that adds them in the Session. Then after each post back you should call a method to recreate the same controls. This way you won't loose those controls data. Give each user control a different ID.
Leniel Macaferi