Вопрос

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?

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top