Asp – Dynamically generated WebUser Control

asp.net

I have two WebUser Controls which are dynamically added to the Webform a number of times according to the user input from a previous page. There are some textboxes and drop downs in the WebUser control. I need to get the values from the textboxes of the WebUsercontrol which are added dynamically. How do I do that?

Best Answer

You should expose the control values as properties in your user control. Then you can loop through the container's Controls collection.

foreach (Control ctl in container.Controls)
{
    if (ctl is MyUserControl)
    {
         MyUserControl uctl = (MyUserControl)ctl;
         // do something with uctl properties, e.g.
         string myString = uctl.Address1;
    }
}
Related Topic