0

到目前为止,我有以下代码显示列表中的列表项(使用自定义 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

我之前没有做太多以编程方式创建表的工作,所以我有点困惑。我已经开始为表格编写一些代码来让我思考,但还没有设法将它们放在一起。

非常感谢您对此提供任何帮助或链接到一个好的教程

非常感谢,

4

2 回答 2

2

如果您想逐个单元格地构建表格,我相信该过程类似于:

Table table = new Table();
TableRow headerRow = new TableRow();
foreach(string field in fields)
{
    TableCell headerCell = new TableCell();
    headerCell.Text = field;
    headerRow.Controls.Add(headerCell);
}
foreach(SPListItem li in listItemColl)
{
    TableRow dataRow = new TableRow();
    foreach(string field in fields)
    {
        TableCell dataCell = new TableCell();
        dataCell.Text = li[field].ToString();
        dataRow.Controls.Add(dataCell);
    }
}
plhDirRankings.Controls.Add(table);

但是,您可以使用数据绑定控件(例如GridView.

例如,在你的情况下,我个人会做类似的事情

// As before until you have your collection.

// Create simple anonymous objects out of your list items.
var items = listItemColl.Cast<SPListItem>()
    .Select(li => new {
        Chambers = li["Title"].ToString(),
        Band = li["Band"].ToString(),
        PeopleRecommended = li["PeopleRecommended"].ToString(),
        Band2 = li["Band2"].ToString()});

// Bind objects to a GridView.
var gridView = new GridView();
plhDirRankings.Controls.Add(gridView);
gridView.DataSource = items;
gridView.DataBind();

我相信这足以得到一个带有列标题的简单表格。

于 2012-10-30T13:09:27.433 回答
0

找到了一种更快更简单的方法来做到这一点:

//creating the table and the table hearers
plhDirRankings.Controls.Add(new LiteralControl("<table><tr><td width='170' 
valign='top'>" + "<strong>Chambers</strong>" + "</td><td width='180' valign='top'>" + 
"<strong>Band</strong>" + "</td><td width='180' valign='top'>" + "<strong>People 
Recommended</strong>" + "</td><td width='145' valign='top'>" + "<strong>Band</strong>" 
+ "</td></tr>"));


 //getting all the list items in the list and displaying them
 foreach (SPListItem listItem in listItemColl)
 {


  //getting listitem values
  chambers = listItem["Title"].ToString();
  band = listItem["Band"].ToString();
  peopleRecommended = listItem["PeopleRecommended"].ToString();
  band2 = listItem["Band2"].ToString();

  //Generating the table content 
  plhDirRankings.Controls.Add(new LiteralControl("<tr><td valign='top'>" + chambers + 
  "</td><td valign='top'>" + band + "</td><td valign='top'>" + peopleRecommended + 
  "</td><td valign='top'>" + band2 + "</td></tr valign='top'>"));



   }

   //closing the table
   plhDirRankings.Controls.Add(new LiteralControl("</table>"));

而是使用使用 for 循环生成表的相当复杂的方法,我刚刚决定在 foreach 循环之外创建表,然后在 foreach 循环内创建表行,然后在 foreach 之外关闭表环形。

更简单,效果很好,感谢大家的帮助,非常感谢

于 2012-10-31T14:15:19.767 回答