Search code examples
sharepoint-2010web-parts

How to add webpartzone in sharepoint 2010 programmatically on C#?


I have a really simple question and probably hard answer. How to add webpartzone into my sharepoint page using C# code? What I did is: I create visual web part project and added this code to page_init event of it:

protected void Page_Init(object sender, EventArgs e)
{
    WebPartZone webPartZone1 = new WebPartZone();
    webPartZone1.AllowLayoutChange = true;
    webPartZone1.EmptyZoneText = "No Data are provided";
    webPartZone1.Enabled = true;
    webPartZone1.HeaderText = "My Custom Zone";
    webPartZone1.Visible = true;

    this.Page.Controls.Add(webPartZone1);
} 

and it's doesn't work. but can give you some idea of what I want. I'm really confused.


Solution

  • I think, what you're trying is a bit wrong, you're trying to add a web part zone in a web part. I don't know if it is possible but you can try to create it via code to a master page.

    I mean, you will not add the created web part zone control in "this.page" but to its master.