跨站点显示列表数据 ListViewWebPart
时间:2010-09-07 来源:liuyuhua
最近做了一个webpart 用来显示sharepoint列表数据,我使用sharepoing的listviewwebpart控件来显示数据
public class CustomListViewWebPart : WebPart
{
protected override void CreateChildControls()
{
base.CreateChildControls();
//添加 ListViewWebPart
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists[“CustomListName”];
SPView wpView = list.Views[“ViewName”];
ListViewWebPart wpListView = new ListViewWebPart();
wpListView.ID = "_wpListView";
wpListView.ListName = list.ID.ToString("B").ToUpper();
wpListView.ViewGuid = wpView.ID.ToString("B").ToUpper();
wpListView.GetDesignTimeHtml();
this.Controls.Add(_wpListView);
}
}
上面的代码完成之后,webpart显示出来后,我需要隐藏toolbar修改代码如下
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists[“CustomListName”];
SPView wpView = list.Views[“ViewName”];
//Code to add ListViewWebPart
ListViewWebPart wpListView = new ListViewWebPart();
wpListView.ID = "_wpListView";
wpListView.ListName = list.ID.ToString("B").ToUpper();
wpListView.ViewGuid = wpView.ID.ToString("B").ToUpper();
//修改SPView 架构设置ToolBar 属性为 “None”
String strtemp = wpView.HtmlSchemaXml;
System.Reflection.PropertyInfo ViewProp = wpListView.GetType().GetProperty("View", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
SPView spTempView = ViewProp.GetValue(wpListView, null) as SPView;
PropertyInfo nodeProp = wpView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
XmlNode node = nodeProp.GetValue(wpView, null) as XmlNode;
XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
if (toolbarNode != null)
{
toolbarNode.Attributes["Type"].Value = "None";
web.AllowUnsafeUpdates = true;
wpView.Update();
web.AllowUnsafeUpdates = false;
}
wpListView.GetDesignTimeHtml();
this.Controls.Add(_wpListView);
上面的代码修改完成后,toolbar是可以不显示了,但列表中选择那视图时,工具栏也同样会不显示,这显然不是我想要,我只是想让我做的webpart中不显示工具栏,不影响视图的显示,通过查找发现listviewwebpart有两个control,有一个是ViewToolBar,于是再次修改代码如下
foreach (Control ctrl in wpListView.Controls)
{
if (ctrl.GetType() == typeof(ViewToolBar))
{
ctrl.Visible = false;
break;
}
}
这时显示的时候工具栏就隐藏了,不影响视图的显示
还有一种方式就是通过修改CSS,也能达到隐藏工具栏,如果一个页面有多个webpart,有不需要隐藏工具栏的,修改样式就不可以了
.ms-menutoolbar
{
display: none;
}