Programmatic adding of WebParts

P

Paal Berggreen

How is a WebPart programmatically added to a WebPartZone, in code-behind?

I would intuitively think that something like this is the way to go:

this.WebPartManager1.Zones["WebPartZone1"].Controls.Add(myPart);

but of course that does not work (WebPartZone does not allow child control),
presumably since the location of the WebParts within the zones etc. are
determined dynamically by the framework, even if the html source places a
specific control in a specific Zone.

So, I want to add a WebPart control to a specific WebPartZone runtime, e.g.
in Page_Load for the page (without having to define the control in the .aspx
design time).

I have played around with various Controls collections, but either they do
not work, or the control is added at the very end of the page, after the
form and body tags :-(

Anyone?


-Paal
 
P

Paal Berggreen

And just to make it clear, this relates to ASP.NET 2.0, not Sharepoint :)

-Paal
 
P

Paal Berggreen

Found it, this does the trick:

this.WebPartManager1.AddWebPart(myPart,
WebPartManager1.Zones["WebPartZone1"], 0);
:)

-Paal
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top