Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

i have a really simple question and probably hard answer.
i want to add webpartzone into my sharepoint page using c# code, how can i do that?
i search all of web and found nothing.
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. im really confused. could anyone help. thanks anyway.

share|improve this question
    
This is a little old to still not have a definite answer ... –  SpYk3HH Oct 11 '12 at 20:50
    
actually i give it up because it have some logical issue i think. as i said in Laurie's comments. –  Jalali Shakib Oct 13 '12 at 20:38
add comment

2 Answers

Try adding your web part zone into the controls collection of a TD on your page instead of directly to the page. Also, make sure you give your new webpartzone an ID.

Your page markup:

<table>
  <tr>
    <td id="TopLeftZone" runat="server"></td>
  </tr>
</table>

Your code-behind:

WebPartZone webPartZone1 = new WebPartZone();
webPartZone1.ID = "myTopLeftZone";
. . .
TopLeftZone.Controls.Add(webPartZone1);
share|improve this answer
1  
thanks for replay. i try this but i got error, because how can i add the webpartzone with webpart. i think my solution is a mistake. –  Jalali Shakib Jul 17 '11 at 13:38
    
Are you saying you got an error when you add your webpart to the page? And, do you mean you are going to solve your problem in another way? If you can add your webpartzone to the aspx page in the markup instead of using C#, that would certainly be a good solution. –  Laurie Jul 17 '11 at 20:17
1  
I'm saying that because when you wanna add your webpart in page you have to add to webpartzone that you selected in combobox and how can you add webpartzone in another one since webpart itself is in another webpartzone that selected?! but thanks for the time you spend. –  Jalali Shakib Jul 19 '11 at 5:27
add comment

I think you will have to update the aspx file content directly (adding the webpart zone template tag).

Using the code you wrote, you won't persist the control addition

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.