3

我需要一个包含标题的纯文本字段、缩略图的图像和 HTML 内容可编辑块的 Web 部件,因此我认为最好的方法是尝试扩展现有的内容编辑器 Web 部件。不幸的是,CEWP 被标记为密封的,所以我不能对它进行子类化。我已经反映并尝试在我自己的自定义 Web 部件中重新创建该功能(有关代码,请参阅问题的结尾),但我的 CEWP 自定义版本的内容并未保留。

有谁知道我该怎么做:

  • 安全地继承 ContentEditableWebPart,或者
  • 在自定义 Web 部件中包含富文本块(包括 RTE 功能区),或
  • 在一个“包装器”Web 部件中托管多个 Web 部件?

提前致谢!

代码如下(阻止它编译的东西是从反射器复制的已被注释掉/更改/删除)

using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace Public.Webparts
{
    [ToolboxItemAttribute(false)]
    [XmlRoot(Namespace="Webparts/ProductItem")]
    public class ProductItemWebPart :System.Web.UI.WebControls.WebParts.WebPart
    {


     // Fields
    private string _content;
    private bool _contentHasToken;
    private string _contentLink;
    private string _partContent;
    private string _partStorage;
    private HtmlGenericControl editableRegion = new HtmlGenericControl();
    private HtmlGenericControl emptyPanel = new HtmlGenericControl();
    private const string EmptyPanelHtmlV4 = "<A href=\"#\" title=\"{0}\" style=\"padding:8px 0px\" class=\"ms-toolbar ms-selectorlink\" >{0}</A>";
    internal const string InputContentClientId = "content";

    // Methods
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public ProductItemWebPart()
    {
        //base.PreRender += new EventHandler(this.OnPreRender);
        if (this.Title.Length == 0)
        {
            this.Title = "Product Item Webpart";
        }
        if (this.Description.Length == 0)
        {
            this.Description = "This web part contains a title, content and image for a product item.";
        }
    }

    private string GetContent()
    {

        if (this._partContent != null)
        {
            return SPHttpUtility.NoEncode(ReplaceTokens(this._partContent));
        }
        return "";
    }

    protected internal string ReplaceTokens(string input)
    {
        string str = string.Empty;
        if (this.WebPartManager != null)
        {
            return SPWebPartManager.ReplaceTokens(HttpContext.Current, SPContext.Current.Web, this, input);
        }
        if (input != null)
        {
            str = input;
        }
        return str;
    }


    private string getEmptyPanelHtml()
    {
        return "<A href=\"#\" title=\"Click to enter content.\" style=\"padding:8px 0px\" class=\"ms-toolbar ms-selectorlink\" >Click to enter content.</A>";
    }



    //private void HttpAsyncCallback(object state)
    //{
    //    ULS.SendTraceTag(0x38393969, ULSCat.msoulscat_WSS_WebParts, ULSTraceLevel.Medium, "ASYNC: Http Callback: UniqueID={0}", new object[] { this.UniqueID.ToString() });
    //    if ((HttpStatusCode.OK != base.GetHttpWebResponse(this._contentLink, out this._partContent)) && (this._content == null))
    //    {
    //        this._partContent = "<p class=\"UserGeneric\">" + SPHttpUtility.HtmlEncode(WebPartPageResource.GetString("CannotRetrieveContent", new object[] { WebPartPageResource.GetString("ContentLinkLiteral") })) + "</p>";
    //    }
    //}

    private bool inEditMode()
    {
        SPWebPartManager currentWebPartManager = (SPWebPartManager) WebPartManager.GetCurrentWebPartManager(this.Page);
        return (((currentWebPartManager != null) && !base.IsStandalone) && currentWebPartManager.GetDisplayMode().AllowPageDesign);
    }

    //[SharePointPermission(SecurityAction.Demand, ObjectModel=true)]
    //public override string LoadResource(string id)
    //{
    //    string str = WebPartPageResource.GetString(id);
    //    if (str != null)
    //    {
    //        return str;
    //    }
    //    return id;
    //}

    [SharePointPermission(SecurityAction.Demand, ObjectModel=true)]
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (this.ShowContentEditable())
        {
            SPRibbon current = SPRibbon.GetCurrent(this.Page);
            if (current != null)
            {
                current.MakeTabAvailable("Ribbon.EditingTools.CPEditTab");
                current.MakeTabAvailable("Ribbon.Image.Image");
                current.MakeTabAvailable("Ribbon.EditingTools.CPInsert");
                current.MakeTabAvailable("Ribbon.Link.Link");
                current.MakeTabAvailable("Ribbon.Table.Layout");
                current.MakeTabAvailable("Ribbon.Table.Design");
                //if (!(this.Page is WikiEditPage))
                //{
                //    current.TrimRTEWikiControls();
                //}
            }
        }
    }

    [SharePointPermission(SecurityAction.Demand, ObjectModel=true)]
    protected override void OnLoad(EventArgs e)
    {
        this.Controls.Add(this.editableRegion);
        this.Controls.Add(this.emptyPanel);
        this.editableRegion.Visible = false;
        this.emptyPanel.Visible = false;
        base.OnLoad(e);
        string str = this.Page.Request.Form[this.ClientID + "content"];
        if ((str != null) && (this._content != str))
        {
            this._content = str;
            try
            {
                SPWebPartManager currentWebPartManager = (SPWebPartManager) WebPartManager.GetCurrentWebPartManager(this.Page);
                Guid storageKey = currentWebPartManager.GetStorageKey(this);
                currentWebPartManager.SaveChanges(storageKey);
            }
            catch (Exception exception)
            {
                Label child = new Label();
                child.Text = exception.Message;
                this.Controls.Add(child);
            }
        }
        if (this.ShowContentEditable())
        {
            string str2;
            //if (this.ContentHasToken)
            //{
            //    str2 = ReplaceTokens(this._content);
            //}
            //else
            //{
            //    str2 = this._content;
            //}

            str2 = this._content;

            this.Page.ClientScript.RegisterHiddenField(this.ClientID + "content", str2);
            this.editableRegion.Visible = true;
            this.emptyPanel.Visible = true;
            this.emptyPanel.TagName = "DIV";
            this.emptyPanel.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
            this.emptyPanel.Controls.Add(new LiteralControl(this.getEmptyPanelHtml()));
            this.emptyPanel.Style.Add(HtmlTextWriterStyle.TextAlign, "center");
            base.Attributes["RteRedirect"] = this.editableRegion.ClientID;
            ScriptLink.RegisterScriptAfterUI(this.Page, "SP.UI.Rte.js", false);
            ScriptLink.RegisterScriptAfterUI(this.Page, "SP.js", false);
            ScriptLink.RegisterScriptAfterUI(this.Page, "SP.Runtime.js", false);
            this.editableRegion.TagName = "DIV";
            this.editableRegion.InnerHtml = str2;
            this.editableRegion.Attributes["class"] = "ms-rtestate-write ms-rtestate-field";
            this.editableRegion.Attributes["contentEditable"] = "true";
            this.editableRegion.Attributes["InputFieldId"] = this.ClientID + "content";
            this.editableRegion.Attributes["EmptyPanelId"] = this.emptyPanel.ClientID;
            this.editableRegion.Attributes["ContentEditor"] = "True";
            this.editableRegion.Attributes["AllowScripts"] = "True";
            this.editableRegion.Attributes["AllowWebParts"] = "False";
            string script = "RTE.RichTextEditor.transferContentsToInputField('" + SPHttpUtility.EcmaScriptStringLiteralEncode(this.editableRegion.ClientID) + "');";
            this.Page.ClientScript.RegisterOnSubmitStatement(base.GetType(), "transfer" + this.editableRegion.ClientID, script);
            if (string.IsNullOrEmpty(this._content))
            {
                this.emptyPanel.Style["display"] = "";
                this.editableRegion.Style["display"] = "none";
            }
            else
            {
                this.emptyPanel.Style["display"] = "none";
                this.editableRegion.Style["display"] = "";
            }
        }
    }

    //private void OnPreRender(object sender, EventArgs e)
    //{
    //    Uri fullURLPath = base.GetFullURLPath(this._contentLink);
    //    if ((fullURLPath != null) && !base.TryToGetFileFromDatabase(fullURLPath, out this._partContent))
    //    {
    //        ULS.SendTraceTag(0x38393968, ULSCat.msoulscat_WSS_WebParts, ULSTraceLevel.Medium, "ASYNC: Begin Fetch: UniqueID={0}", new object[] { this.UniqueID.ToString() });
    //        base.RegisterWorkItemCallback(new WaitCallback(this.HttpAsyncCallback), null);
    //    }
    //}

    [SharePointPermission(SecurityAction.Demand, ObjectModel=true)]
    //protected override void RenderWebPart(HtmlTextWriter writer)
    protected override void Render(HtmlTextWriter writer)
    {
        //if (this.ShowContentEditable() && base.WebPartManager.IsAllowedToScript(this))
        if (this.ShowContentEditable())
        {
            base.Render(writer);
        }
        else
        {
            writer.Write(this.GetContent());
        }
    }

    //[SharePointPermission(SecurityAction.Demand, ObjectModel=true)]
    //protected internal override bool RequiresWebPartClientScript()
    //{
    //    return true;
    //}

    //public bool ShouldSerializeContent()
    //{
    //    if (!base.SerializeAll)
    //    {
    //        return (this.WebPartDefault._content != this._content);
    //    }
    //    return true;
    //}

    //public bool ShouldSerializeContentHasToken()
    //{
    //    return ((this.WebPartDefault._contentHasToken != this._contentHasToken) && !base.SerializeAll);
    //}

    //public bool ShouldSerializeContentLink()
    //{
    //    if (!base.SerializeAll)
    //    {
    //        return (this.WebPartDefault._contentLink != this._contentLink);
    //    }
    //    return true;
    //}

    public bool ShouldSerializePartStorage()
    {
        //if (!base.SerializeAll)
        //{
            //return (this.WebPartDefault._partStorage != this._partStorage);
        //}
        return true;
    }

    internal bool ShowContentEditable()
    {
        return (((SPContext.Current.Web.UIVersion > 3) && (this._partContent == null)) && this.inEditMode());
    }


    //[XmlElement("ContentHasToken", IsNullable=false), Browsable(false), WebPartStorage(Storage.Shared)]
    //public bool ContentHasToken
    //{
    //    get
    //    {
    //        return this._contentHasToken;
    //    }
    //    set
    //    {
    //        this._contentHasToken = value;
    //    }
    //}



    //private string EncodedInvalidContentError
    //{
    //    get
    //    {
    //        string str2;
    //        string str = "<a id=\"" + this.ID + "HelpLink\" href=\"javascript:HelpWindowUrl('" + SPHttpUtility.NoEncode("sts/html/dpvwpabt.htm") + "');\">" + SPHttpUtility.HtmlEncode(WebPartPageResource.GetString("InvalidContentErrorHelpLink")) + "</a>";
    //        if ((this.Context != null) && Utility.BrowserIsIE(this.Context.Request.Browser))
    //        {
    //            str2 = "InvalidContentError";
    //        }
    //        else
    //        {
    //            str2 = "InvalidContentErrorDL";
    //        }
    //        return ("<p><div class=\"UserGeneric\">" + string.Format(CultureInfo.InvariantCulture, SPHttpUtility.HtmlEncodeAllowSimpleTextFormatting(WebPartPageResource.GetString(str2)), new object[] { str }) + "</div></p>");
    //    }
    //}

    [Resources("PartStorageLiteral", "Advanced", "PartStorage"), WebPartStorage(Storage.Personal), XmlElement("PartStorage", IsNullable=false)]
    public string PartStorage
    {
        get
        {
            //return Utility.GetMemberString(this._partStorage);
            return this._partStorage ?? String.Empty;
        }
        set
        {           
            //Utility.SetMemberString(ref this._partStorage, value);

            if (value != null && value.Length > 0)
            {
                this._partStorage = value;
            }
            else
            {
                this._partStorage = null;
            }
        }
    }

    //internal ContentEditorWebPart WebPartDefault
    //{
    //    get
    //    {
    //        return (ContentEditorWebPart) base.WebPartDefault;
    //    }
    //}


    }
}
4

3 回答 3

5

认为这可能会解决您的问题 - 当您取出 Reflector 时,您走在了正确的轨道上 :)

http://zootfroot.blogspot.com/2010/09/develop-custom-editable-visual-web-part.html

于 2010-09-24T10:30:03.047 回答
1

您可以使用 ControlAdapter,如本文所述:http: //blog.mastykarz.nl/inconvenient-content-editor-web-part/

于 2010-09-18T13:59:27.327 回答
0

为什么不创建“可视 Web 部件”并使用富文本编辑器(如 Telerik 或其他)?我认为尝试对 CEWP 的代码进行逆向工程可能是矫枉过正,并且可能违反 SharePoint 的许可证。

于 2010-09-16T02:18:09.980 回答