到目前为止,我有以下代码显示列表中的列表项(使用自定义 Web 部件属性来获取列表 url 和列表名称)。
显示列表项的代码:
if(this.WebPart.ListUrl != null && this.WebPart.ListName != null &&
this.WebPart.AwardYear != null)
{
//getting custom properties values
string listURL = this.WebPart.ListUrl.ToString();
string listName = this.WebPart.ListName.ToString();
string awardYear = this.WebPart.AwardYear.ToString();
using (SPSite site = new SPSite(listURL))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPList list = web.Lists[listName]; //name of the list
//CAML query to filter list items by year and then order by year in descending order
SPQuery awardsYear = new SPQuery();
awardsYear.Query = @"<Where><Eq><FieldRef Name='Year'/><Value Type='Text'>" +
awardYear + @"</Value></Eq></Where>" + "<OrderBy><FieldRef Name='Year'
Ascending='False' /></OrderBy>";
SPListItemCollection listItemColl = list.GetItems(awardsYear);
//code for generating the table goes here EXPERIMENTAL
Table table1 = new Table();
TableRow tableRow = new TableRow();
TableCell tableCell = new TableCell();
int numberOfColumns = 4; //number of columns for the chambers table
for (int x = 0; x < numberOfColumns; x++)
{
//Table columns created here need to be added somehow to the table above
}
//getting all the list items in the list and displaying them
foreach (SPListItem listItem in listItemColl)
{
//For each of the list items create the table rows
//The below needs to be put into a table generated programatically
chambers = listItem["Title"].ToString();
band = listItem["Band"].ToString();
peopleRecommended = listItem["PeopleRecommended"].ToString();
band2 = listItem["Band2"].ToString();
//placeholders used to display the results
plhDirRankings.Controls.Add(new LiteralControl("Chambers: " + chambers + "<br/>"));
plhDirRankings.Controls.Add(new LiteralControl("Band: " + band + "<br/>"));
plhDirRankings.Controls.Add(new LiteralControl("People Recommended: " +
peopleRecommended + "<br/>"));
plhDirRankings.Controls.Add(new LiteralControl("Band: " + band2 + "<br/>"));
}
}
catch (Exception err)
{
plhDirRankings.Controls.Add(new LiteralControl(err.ToString()));
}
}
}
}
以编程方式生成表格以显示列表项的最简单方法是什么,如下所示:
Chambers | Band | PeopleRecommended | Band2
--------------------------------------------
item1 | item1 | item1 | item1
item2 | item2 | item2 | item2
我之前没有做太多以编程方式创建表的工作,所以我有点困惑。我已经开始为表格编写一些代码来让我思考,但还没有设法将它们放在一起。
非常感谢您对此提供任何帮助或链接到一个好的教程
非常感谢,