我需要一个webpart,它有一个标题的纯文本字段,一个缩略图的图像和一个HTML内容可编辑的块,所以我认为最好的方法是尝试扩展现有的内容编辑器Web部件。不幸的是,CEWP标记为密封,因此我不能将其子类化。我已经反思并尝试在我自己的自定义webpart中重新创建功能(请参阅代码问题的结尾),但我的自定义CEWP版本的内容不会保留。
有谁知道我怎么做:
提前致谢!
代码如下(停止编译的东西已被注释掉/更改/删除)
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;
// }
//}
}
}
答案 0 :(得分:5)
认为这可以解决您的问题 - 当您将Reflector输出时,您处于正确的轨道上:)
http://zootfroot.blogspot.com/2010/09/develop-custom-editable-visual-web-part.html
答案 1 :(得分:1)
您可以使用ControlAdapter,如本文所述:http://blog.mastykarz.nl/inconvenient-content-editor-web-part/
答案 2 :(得分:0)
为什么不创建“Visual Web Part”并使用富文本编辑器(如Telerik或其他人)?我认为尝试对CEWP的代码进行反向工程可能是过度的,并且可能违反了SharePoint的许可。