创建页面时向 WebPartPage 添加描述
Posted
技术标签:
【中文标题】创建页面时向 WebPartPage 添加描述【英文标题】:Adding description to WebPartPage when creating the page 【发布时间】:2011-06-03 15:22:04 【问题描述】:我正在按照这个 (http://msdn.microsoft.com/en-us/library/ms450826.aspx) 方法添加 webpartpage (samplewpp.aspx) 并且它可以工作。但是,我还需要添加一行描述。怎么样?
【问题讨论】:
描述应该在哪里?在页面上?在页面的元数据中? 在加载页面(samplewpp.aspx)的页面上 【参考方案1】:您需要向页面添加内容编辑器 Web 部件 (CEWP),然后向其中添加您的描述。 CEWP 允许您将文本/html 放到页面上。
要以编程方式执行此操作,请关注 something like this code by Razi bin Rais :-
AddAndFillCEWP("http://server","/" ,"/Pages/blank.aspx","this text is adding via code","Header","CEWP WebPart");
private void AddAndFillCEWP(string siteUrl, string webName, string pageUrl, string textCEWP, string zoneId, string title)
SPSecurity.RunWithElevatedPrivileges(delegate()
using (SPSite spSiteTest = new SPSite(siteUrl))
using (SPWeb web = spSiteTest.OpenWeb(webName))
try
web.AllowUnsafeUpdates = true;
SPFile file = web.GetFile(pageUrl);
if (null != file)
using (SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
if (null != mgr)
//create new webpart object
ContentEditorWebPart contentEditor = new ContentEditorWebPart();
//set properties of new webpart object
contentEditor.ZoneID = zoneId;
contentEditor.Title = title;
contentEditor.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
contentEditor.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleAndBorder;
//Add content to CEWP
XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlElement = xmlDoc.CreateElement("Root");
xmlElement.InnerText = textCEWP;
contentEditor.Content = xmlElement;
contentEditor.Content.InnerText = xmlElement.InnerText;
//Add it to the zone
mgr.AddWebPart(contentEditor, contentEditor.ZoneID, 0);
web.Update();
finally
web.AllowUnsafeUpdates = false;
);
【讨论】:
以上是关于创建页面时向 WebPartPage 添加描述的主要内容,如果未能解决你的问题,请参考以下文章