0

I am trying to create a unique component that has two Content Editor Web Parts. One of these will contain embed code showing a video, while the other one will contain description as HTML.

The idea is to keep both controls separate, but treat this as a unique Web Part so adding it to different pages will be easier.

Adding a Web Part Zone to a Web Part sounds weird, but I tried it anyway and I keep getting different errors. Is this even possible?

Would this be possible using a User Control instead?


UPDATE

I have managed to add a CEWP to my Visual Web Part programmatically by adding the code below to the OnInit event:

web.AllowUnsafeUpdates = true;

using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
{
    XmlElement p = new XmlDocument().CreateElement("p");
    p.InnerText = "Hello World";
    ContentEditorWebPart cewp = new ContentEditorWebPart();
    cewp.Content = p;
    wpmgr.AddWebPart(cewp, String.Format("{0}_webPartZone", this.ID), 0);
    wpmgr.SaveChanges(cewp);
}

web.AllowUnsafeUpdates = false;

The isse now is that this is triggered every time the page loads, so a new CEWP is added to my page. How can I determine whether I already have a CEWP or not? Should I iterate through the WebParts on the SPLimitedWebPartManager?

2
  • Yes you can easily create a Visual Web Part and reference two Content Editor web parts inside it.. Nov 28, 2014 at 20:54
  • Thank you for the prompt reply. I would really much appreciate if you could provide a link to any articles with further information about this.
    – Gonzalo
    Nov 28, 2014 at 20:57

1 Answer 1

0

The code you tried is basically getting the current page's webpart manager and then adding new webpart to it.

The ideal solution to your problem is by extending System.Web.UI.WebControls.WebParts.WebPart and overriding CreateChildControls

Here is one thread that explains this

https://stackoverflow.com/questions/419632/possible-to-load-a-web-part-inside-another

4
  • Thank you Amal. The issue I am facing now is that the CEWP if added programmatically, cannot be edited. Any thoughts on why is this happening?
    – Gonzalo
    Dec 2, 2014 at 14:01
  • I think you should change your approach and instead of using Content Editor try to use RichHtmlEditor control. This allows users to edit and add HTML information. Dec 2, 2014 at 14:04
  • You mean a RichHtmlField?
    – Gonzalo
    Dec 2, 2014 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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