嗨,我有一个 webpart,我在后面的代码中创建了一个表格,以在两个列中显示“奖项”列表的列表项的值,其中每个奖项在两个列中并排显示。我正在使用 foreach 循环来获取所有列表项值并同时生成表。
问题是由于某种原因并非所有列表项值都被显示。列表项值也在输出中重复。以下是我当前拥有的代码片段:
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPList awardsList = web.Lists["Awards"];
SPListItemCollection listItemCollection = awardsList.Items;
int modCounter = 0;
foreach (SPListItem oListItem in listItemCollection)
{
modCounter += 1;
awardYear = oListItem["Year"].ToString();
awardCategory = oListItem["Category"].ToString();
awardNomWon = oListItem["NominatedWon"].ToString();
awardLogo = oListItem["Logo"].ToString(); //need to fingure out how to display images
Table tbl = new Table();
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;
//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>"));
//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>"));
//adding Rows
tblRow.Cells.Add(tblCellLeft);
tblRow.Cells.Add(tblCellRight);
tbl.Rows.Add(tblRow);
if (modCounter % 2 == 0)
{
//values from tblCellLeft
PlaceHolder6.Controls.Add(tbl);
}
else
{
//values from the tblRightCell
PlaceHolder2.Controls.Add(tbl);
}
}
}
catch (Exception err)
{
PlaceHolder3.Controls.Add(new LiteralControl(err.ToString()));
}
}
}
});
我不确定问题是什么。任何帮助将不胜感激。
谢谢