Programmatically adding a WebPart to a content page

K

kjartan93a

The following code is on my master page, site.master.cs:

// programatically add a WebPart from a User Control file
Control uc = this.LoadControl(@"ucLotListSimple.ascx");
uc.ID = "MyWp";
GenericWebPart MyWp = WebPartManager1.CreateWebPart(uc);
WebPartManager1.AddWebPart(wp2, wpzUpper, 1);

The last line above works fine only if the WebPartZone wpzUpper is
located on the master page itself.

What is the proper way to reference wpzUpper if it is on the content
page "Default.aspx"?

Thanks.
 
G

Guest

kjartan93

Your not showing us all the code? wp2 mywp

You can find the zone if you know its id with the zones collection of the
webpartmanager and you can access the webpartmanager through a shared
function.

webpartmanager wpm = webpartmanager.getcurrentwebpartmanager(page);
webpartzone wpz=wpm.zones("wpzUpper");
wpm.AddWebPart(wp2, wpz, 1);

I didn't see wp2 in your code so your on your own.

Good Luck
DWS
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top