我想循环浏览我使用 C# 代码创建的表,并显示从表格中格式化的 Sharepoint 列表中提取的数据。这是我想出的代码:
protected override void RenderContents(HtmlTextWriter output)
{
try
{
SPSite thisSite = SPControl.GetContextSite(Context);
using (SPWeb topWeb = thisSite.OpenWeb("/"))
{
SPList newsList = topWeb.Lists["Headlines"];
SPQuery query = new SPQuery();
query.ExpandRecurrence = true;
query.Query = NewsListCamlQuery;
query.RowLimit = 2;
SPListItemCollection coll = newsList.GetItems(query);
output.Write("<table border='0' style='width: 100%;'><tbody>");
for (int i = 0; i < 2; i++)
{
output.Write("<tr>");
for (int j = 0; j < 3; j++)
{
output.Write("<td>");
foreach (SPListItem item in coll)
{
int newsID = int.Parse(item.ID.ToString());
output.Write("<hr/>");
output.Write("<table style='width: 100%; height: 143px;'><tbody>");
output.Write("<tr><td valign='top'><h4>");
output.Write(item["Title"].ToString());
output.Write("</h4></td></tr>");
output.Write("<tr><td valign='top'><h4 class='ms-rteElement-H4B'><span style='font-size: 8pt;'>");
output.Write(item["Sub_x0020_Heading"].ToString());
output.Write("</span></h4></td></tr>");
output.Write("<tr><td valign='top'><span style='font-size: 8pt;'>");
output.Write("<img class='image' src='");
//output.Write(item["Photo"].ToString());
output.Write("' Width='64px' Height='48px' Border='0' alt='' style='margin: 5px;'/>");
output.Write(item["Details"].ToString());
output.Write("</span></td></tr>");
output.Write("<tr><td valign='top'>");
output.Write("<a class='mt-linkMore' href='/Lists/Headlines/DispForm.aspx?ID=");
output.Write(newsID);
output.Write("' <span style='font-size: 6pt;'>Read More</span></a></td></tr>");
output.Write("</tbody></table>");
}
output.Write("</td>");
}
output.Write("</tr>");
}
output.Write("</tbody></table>");
}
}
catch (Exception exception)
{
output.Write("Error : " + exception.Message);
}
}
此代码当前正在每个单元格中重写相同的记录。我希望它显示从 Sharepoint 列表中提取的六条记录。