读书人

问两个弱弱的关于XsltListViewWebPart

发布时间: 2013-03-06 16:20:31 作者: rapoo

问两个弱弱的关于XsltListViewWebPart和ContentEditorWebPart的问题
我在一个WebPart中加了如下代码,把当前网站上所有LIST用XsltListViewWebPart显示出来
XsltListViewWebPart xlvWebPart = null;
foreach (SPList currList in currWeb.Lists)
{
if (currList.BaseType.CompareTo(SPBaseType.GenericList) == 0)
{
xlvWebPart = new XsltListViewWebPart();
xlvWebPart.ListId = currList.ID;
xlvWebPart.Title = currList.Title;
this.LayoutPanelMain.Controls.Add(xlvWebPart);
}
}
不过遇到一个问题,就是显示出的LIST 下面没有toolbar 显示 add new item链接,请问怎么让他显示,
还有个问题怎么用C#代码给ContentEditorWebPart里增加html内容
[解决办法]
1. 将某个list作为web part添加到某个页面应该用如下代码:
http://blog.brianfarnhill.com/2008/03/add-a-list-view-web-part-to-a-page-with-code
2. ContentEditorWebPart只能写HTML和Javascript, CSS等前台代码的。
[解决办法]
参考这段代码:

if (myItem.Name.ToUpper() == "DEFAULT.ASPX")
{
SPLimitedWebPartManager webPartCollection = myItem.File.GetLimitedWebPartManager(PersonalizationScope.Shared);
PublishingPage myPage = PublishingPage.GetPublishingPage(myItem);
myPage.CheckIn("if checked out");
myPage.CheckOut();
int u = webPartCollection.WebParts.Count;
for(int i=0;i<=u;i++){
Microsoft.SharePoint.WebPartPages.WebPart webpt = (Microsoft.SharePoint.WebPartPages.WebPart)webPartCollection.WebParts;
if (webpt.GetType().ToString() == "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart"){
//Create an XmlElement to hold the value of the Content property.
ContentEditorWebPart ceWebPart = new ContentEditorWebPart();

// Create an XmlElement to hold the value of the Content property.
XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlElement = xmlDoc.CreateElement("Content");
xmlElement.InnerText = ((
ContentEditorWebPart)webpt).Content.InnerText.ToString();


xmlElement.InnerText = xmlElement.InnerText.ToString().Replace(
"hotmail", "Yahoo");
//Set the Content property to the XmlElement.
ceWebPart.Content = xmlElement;
((
ContentEditorWebPart)webpt).Content = ceWebPart.Content;webPartCollection.Web.AllowUnsafeUpdates = true;
webPartCollection.SaveChanges(webpt);
webPartCollection.Web.AllowUnsafeUpdates = false;
}
}
myPage.Update();
myPage.CheckIn("");
if (myPage.ListItem.ParentList.EnableModeration)
{
myPage.ListItem.File.Approve("");
}
}

读书人网 >行业软件

热点推荐