2010-05-05 59 views
0

我正在使用下面的代碼將SummaryLinkWebPart添加到頁面,並且還添加了幾條指向該wehbpart的鏈接。我現在可以在頁面上看到webpart,但它裏面沒有任何鏈接。有人知道代碼有什麼問題嗎?以編程方式添加SummaryLinkWebPart不顯示鏈接

var wpm = web.GetLimitedWebPartManager("Pages/default.aspx", PersonalizationScope.Shared); 

SummaryLinkWebPart slwp = new SummaryLinkWebPart(); 
    for (int counter = 0; counter < list.ItemCount; counter++) 
    { 
     urlField = list.Items[counter]["URL"].ToString().Split(','); 

     SummaryLink link = new SummaryLink(urlField[1].Trim()); 
     slwp.SummaryLinkValue.SummaryLinks.Add(link); 
     slwp.SummaryLinkValue.SummaryLinks[counter].OpenInNewWindow = true; 
     slwp.SummaryLinkValue.SummaryLinks[counter].LinkUrl = urlField[0].Trim(); 
     slwp.SummaryLinkValue.SummaryLinks[counter].Description = urlField[1]; 
     slwp.Style = "Image on left"; 
     Console.WriteLine(link.LinkUrl + link.Title); 
    } 

    wpm.AddWebPart(slwp, lvwp.ZoneID, slwp.ZoneIndex + 1); 

回答

2

嗯,這似乎是黑客,但可以解決這個問題再次重新分配SummaryLinkValue房產其價值的唯一的事情。

slwp.SummaryLinkValue = slwp.SummaryLinkValue;

+0

我不喜歡它,但它的工作原理!謝謝! – 2014-04-28 18:47:05

1

使用

SPLimitedWebPartManager wpManager = page.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); 

那麼當完成呼叫page.Update()。像這樣的東西

SPFile page = web.GetFile(string.Format("Pages/{0}", this.FileName.Replace("html", "aspx"))); 
SPLimitedWebPartManager wpManager = page.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); 
SummaryLinkWebPart webpart = new SummaryLinkWebPart(); 

if (!string.IsNullOrEmpty(RelatedTopic1)) 
    webpart.SummaryLinkValue.SummaryLinks.Add(GetSummaryLink(web, pages, RelatedTopic1)); 
if (!string.IsNullOrEmpty(RelatedTopic2)) 
    webpart.SummaryLinkValue.SummaryLinks.Add(GetSummaryLink(web, pages, RelatedTopic2)); 
if (!string.IsNullOrEmpty(RelatedTopic3)) 
    webpart.SummaryLinkValue.SummaryLinks.Add(GetSummaryLink(web, pages, RelatedTopic3)); 
if (!string.IsNullOrEmpty(RelatedTopic4)) 
    webpart.SummaryLinkValue.SummaryLinks.Add(GetSummaryLink(web, pages, RelatedTopic4)); 
if (webpart.SummaryLinkValue.SummaryLinks.Count > 0) 
{ 
    wpManager.AddWebPart(webpart, "BottomPanel", 0); 
    page.Update(); 
} 
相關問題