How to add controls to webpart zone in runtime

I get the following error 'System.Web.UI.WebControls.WebParts.WebPartZone' does not allow child controls.

Regards,
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // set the display mode to "Design" to allow drag and drop  
                WebPartDisplayMode mode = WebPartManager1.SupportedDisplayModes["Design"];
                if (mode != null)
                {
                    WebPartManager1.DisplayMode = mode;
                }
                WebFleetEntities x = new WebFleetEntities();
                var query = from c in x.Vehicles
                            where c.VehGroups.GroupID == 20
                            select c;
                BusUserControl bus;
                foreach (Vehicles veh in query)
                {
                    bus = new BusUserControl();
                    bus.BusCode = veh.VCode;
                    WebPartZone1.Controls.Add(bus);
                    
                }
 
 
            }  
 
        }

Open in new window

saudiprogrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tbsgadiCommented:
0
saudiprogrammerAuthor Commented:
i want to add controls to a webpart not add webpart to a web page.
0
carlnorrbomCommented:
Hi,

Try changing Your code to:

foreach (Vehicles veh in query)
    integer i = 0;
    {
        bus = new BusUserControl();
        bus.BusCode = veh.VCode;
        WebPartManager1.AddWebPart(WebPartManager1.CreateWebPart(bus), WebPartZone1, i);
        i++;
    }

/Carl.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

saudiprogrammerAuthor Commented:
The specified control of type 'NaghiSolutions.BusUserControl' does not have an ID.
0
carlnorrbomCommented:
Hi,

Try changing it to:

foreach (Vehicles veh in query)
    integer i = 0;
    {
        bus = new BusUserControl();
        bus.BusCode = veh.VCode;
       bus.ID = "Bus" + i.ToString();
        WebPartManager1.AddWebPart(WebPartManager1.CreateWebPart(bus), WebPartZone1, i);
        i++;
    }

and see if that helps.

/Carl.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
saudiprogrammerAuthor Commented:
The "path" argument cannot be empty if the "type" argument is UserControl.
0
carlnorrbomCommented:
Hi,

Well, since I have no knowledge of how Your user control is constructed it's hard for me to guide You on how to instantiate it and create a webpart from it. How do you normally create and instantiate Your user control programatically? Use that approach here as well.

Adding a control to a webpartzone however is done using the methodology I have shown You.

/Carl.
0
saudiprogrammerAuthor Commented:
 public partial class BusUserControl : System.Web.UI.UserControl
    {
        private string _BusCode;

        public string BusCode
        {
            get { return _BusCode; }
            set { _BusCode = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            VehicleCode.Text = BusCode;
        }
    }
here is the user code
i created a usercontrol with image (showing a picture of a bus) and a label (VehcileCode is the label)
0
saudiprogrammerAuthor Commented:
bus = (BusUserControl)LoadControl("BusUserControl.ascx");

added this line before

bus = new BusUserControl();

and the issue is solved

Thanks
0
carlnorrbomCommented:
Hi,

Good, hope it works for You with the rest of your project!

/Carl.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.