views:

54

answers:

4

Long story short, I would like to take my usercontrols that are loaded dynamically and stick them into a list that references the actual object that has been loaded. So I can go into the list and pull results from the usercontrol. I would like to use mycontrol.GetResult() and it will then reference the control and grab the results from the form that has been filled out, which the results will be returned as a string. I do not want it to initialize a new control of the same type because I will not be able to receive my results then. Any suggestions?

Thanks!

+4  A: 

Just create a list of Controls:

var controls = new List<Control>();

foreach(var control in Page.Controls)
{
    controls.Add(control);
}

You can then use that list to reference each of the controls as needed (this is a simple example...your code to populate the list will most likely be more complicated).

Justin Niessner
A: 

You can make use of Dictionary where key will be the typeof usercontrol and value will be the actual control and when you add a new control check key with contains method

saurabh
A: 

Who adds the controls programmatically? THe page or a user control? That component could do it. Otherwise, another trick is say a user control dynamically loads other user controls. You could give the page an interface definition say IDynamicControlHolder that has a List type, and do in the dynamic control (or somewhere else).

if (this.Page is IDynamicControlHolder)
   // change this to the appropriate reference
   ((IDynamicControlHolder)this.Page).DynamicControls.Add(this); 
Brian
I have a wizard control, onInit it searches a directory and adds all of the user forms that have been dropped in it. So the wizard step is dynamically loaded then the usercontrol is dynamically loaded into the wizard step. I have a list already but it does not hold the the control just the object I have created for the control properties to be stored.
Tony
"it does not hold the the control just the object" - by control I assume you mean user control, not wizard step. The description above I gave you is a self-registering way to do things; as long as the page implements an interface (which requires returning a List<UserControl> instance named DynamicControls), the user control, in OnLoad or maybe even OnInit would work, would register itself.
Brian
A: 

So I already had similar code created and the controls loaded into a list but I was not sure how to get the results from the forms placed back into my object. After seeing code examples I realized I was already doing that so there had to be a better way to get what I want. I manipulated one of my existing methods and now I use it to load the results into my object when the user goes to the verification screen. This works great, thank you for confirming for me I was doing it right.

Tony