我正在使用以下代码以编程方式将网页部件添加到网页中, 但是我有一个问题,在页面回传后,网页部分是 在页面上看不到,我需要刷新页面以获取Web部件 可见。
SPSite site = new SPSite("http://syngdcds0032:23547");
site.AllowUnsafeUpdates = true;
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["Assets"];
SPView setView = list.Views["VVV"];
// Instantiate the web part
ListViewWebPart wp = new ListViewWebPart();
wp.ZoneID = "Left";
wp.ListName = list.ID.ToString("B").ToUpper();
//wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();
wp.ViewGuid = setView.ID.ToString("B").ToUpper();
// Get the web part collection
SPWebPartCollection coll =
web.GetWebPartCollection("http://syngdcds0032:23547/Pages/AssetSearch1.aspx",
Storage.Shared);
// Add the web part
coll.Add(wp);
答案 0 :(得分:1)
一些建议
尝试使用LimitedWebpartManager类(http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.splimitedwebpartmanager_methods.aspx)将webpart添加到页面。在这种情况下,您不必在每次调用中将webpart添加到页面中,您必须在页面的生命周期中执行一次。 代码看起来像这样
Dim op As WebPartPages.SPLimitedWebPartManager
op = oWeb.GetLimitedWebPartManager("default.aspx", Web.UI.WebControls.WebParts.PersonalizationScope.Shared)
oWP = New WebPartPages.ListViewWebPart
oWP.ListName = oWeb.Lists("Workflow tasks").ID.ToString("B").ToUpper()
oWP.AllowClose = False
oWP.AllowHide = False
oWP.AllowMinimize = False
oWP.AllowZoneChange = True
oWP.ExportMode = Web.UI.WebControls.WebParts.WebPartExportMode.All
oWP.Title = "Darba uzdevumi"
oWP.ChromeType = Web.UI.WebControls.WebParts.PartChromeType.TitleOnly
oWP.ViewGuid = oWeb.Lists("Workflow tasks").Views("All Items").ID.ToString("B").ToUpper()
oWP.ZoneID = "Right"
op.AddWebPart(oWP, "Right", 1)