到目前爲止,我有以下代碼顯示列表中的列表項目(使用自定義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
我沒有做太多與之前編程創建表,所以我有點困惑。我已經開始了一些表格的代碼,讓我思考,但沒有設法把它放在一起。
在這個或也許是一個很好的教程鏈接,將不勝感激
非常感謝,
嗨協助下,你的第一個例子看起來像去爲病房一次工作,我會加入CSS樣式該表後的方式。無論如何,我對於foreach循環內部「字段中的字符串字段」來自哪裏有點困惑?謝謝 –
'new [] {「Title」,「Band」,「PeopleRecommended」,「Band2」}''會做到這一點,儘管您可以爲我使用它的兩個地方(列標題/列表字段名稱)有不同的地方。 – Rawling
除了創建自定義Linq對象外,還可以使用[GetDataTable方法](http://msdn.microsoft.com/zh-cn/library/microsoft.sharepoint.splistitemcollection.getdatatable.aspx)返回一個DataTable可以綁定到一個GridView。 –