我無法按正確的順序顯示列表項的值。以下是在列表中的字段(與他們的實際系統名稱):排序列表項目(最新的一個)
-Title - 類別 -NominatedWon -logo
「標題」字段包含年份值例如,「2011」,「 2010" 等
//getting the awards list and extracting correct values from the necesary fields
//asp running with elevated privilegs
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPList awardsList = web.Lists["Awards"];
SPListItemCollection listItemCollection = awardsList.Items;
//Creating the table
Table tbl = new Table();
//foreach (SPListItem oListItem in listItemCollection)
int x = listItemCollectionI.Count;
for(int i = 0; (i * 2) < x; i++) // divide total item collection by two, each loop
iteration, add two awards to a row (left cell, right cell)
{
// get listItemCollection[i];
//Create table rows, table cells
int leftIndexer = i * 2;
int rightIndexer = (i * 2) + 1;
if (leftIndexer == x)
{
break;
}
TableRow tblRow = new TableRow();
TableCell tblCellLeft = new TableCell(); //for the awards in the first column
TableCell tblCellRight = new TableCell(); //for the awards in the second column
tblCellLeft.VerticalAlign = VerticalAlign.Top;
tblCellLeft.HorizontalAlign = HorizontalAlign.Center;
tblCellLeft.CssClass = ("style5");
tblCellRight.VerticalAlign = VerticalAlign.Top;
tblCellRight.HorizontalAlign = HorizontalAlign.Center;
// get the values
awardYear = listItemCollection[leftIndexer]["Title"].ToString();
awardCategory = listItemCollection[leftIndexer]["Category"].ToString();
awardNomWon = listItemCollection[leftIndexer]["NominatedWon"].ToString();
if(listItemCollection[leftIndexer]["Logo"] != null)
awardLogo = (string)listItemCollection[leftIndexer]["Logo"];
// add to left cell
//values for the left column
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear +
"</div>"));
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory
+ "</div>"));
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon +
"</div>"));
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" +
awardLogo.Replace(",", "") + "</div>"));
// add left cell to row
tblRow.Cells.Add(tblCellLeft);
if (rightIndexer < x) // if this item exists in the collection (prevent bug with odd
number of awards)
{
// get the values
awardYear = listItemCollection[rightIndexer]["Title"].ToString();
awardCategory = listItemCollection[rightIndexer]["Category"].ToString();
awardNomWon = listItemCollection[rightIndexer]["NominatedWon"].ToString();
if (listItemCollection[rightIndexer]["Logo"] != null)
awardLogo = (string)listItemCollection[rightIndexer]["Logo"];
// add to right cell
//Values for the right column
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear +
"</div>"));
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory
+ "</div>"));
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon +
"</div>"));
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" +
awardLogo.Replace(",", "") + "</div>"));
// add right cell to row
tblRow.Cells.Add(tblCellRight);
}
// add row to table
tbl.Rows.Add(tblRow);
}
PlaceHolder6.Controls.Add(tbl); // add table outside of loop
}
catch (Exception err)
{
PlaceHolder3.Controls.Add(new LiteralControl(err.ToString()));
}
}
}
});
所以輸出由於某種原因,似乎表明這兩個最新的列表中的項目如列表項‘2012’,並列出項目‘2011’在底部顯示。理論上這些應該顯示在頂部,其次是其餘的列表項。
任何對此的建議將不勝感激!
感謝
感謝那些很好地訣竅。 – 2012-01-17 14:33:31
感謝Paul Leigh提供的解決方案!一個簡短的說明,但。爲了讓它在我的環境中工作,我必須在GetItems之前取出「.Items」:SPListItemCollection listItemCollection = awardsList.GetItems(q); – user3868912 2014-07-23 13:11:46
SPListItemCollection listItemCollection = awardsList.GetItems(q); – 2015-11-11 10:45:17