2012-02-13 30 views
0

現在、2つのコンテンツプレースホルダを使用してリストアイテムのタイトルと概要を表示しています。 Currenlty私は、2つのリスト項目のタイトルと要約のみを表示しています。プレースホルダを使用する代わりに、forループで指定されている数のリスト項目のタイトルと要約を表示する行とセルを生成する、コードの背後にテーブルを作成できるようにします。この意志とコード内でforループを使用してテーブルを作成する

//for loop to iterate through the list retrieving the required amount of lits items 
//the number of times to iteratie through the loop will be specified by the user using 
//the custom property 
if (this.WebPart != null & this.WebPart.ListName != null & 
!this.WebPart.OverviewArticle) 
{ 
string Listname = this.WebPart.ListName; //get the value of the ListName custom 
property 
int publicationNumberofitems = this.WebPart.pubNumOfItems; //get the value of the 
                  //pubNumOfItems as int 

//since the first iten in the list is 0 (making this variable -1 cause the correct 
//amount of details to be displayed based on the number 
//provided via the pubNumOfItems custom property. So if a value of 2 is provided then 
//only two items will be displayed and so on. 
int listItemsNumber = publicationNumberofitems - 1; 

SPWeb web = SPContext.Current.Web; 
SPList list = web.Lists[Listname]; 

SPListItemCollection collListItems = list.Items; 

//creating a table to hold the list items in the for loop. This will replace the use 
//of the placeholders 
Table table1 = new Table(); 
TableRow tr = new TableRow(); 
TableCell tc = new TableCell(); 



//for loop to iterate through the list retrieving the required amount of lits items 
//the number of times to iteratie through the loop will be specified by the user using 
//the custom property 
for (int i = 0; i < listItemsNumber && i < collListItems.Count; i++) 
{ 
SPListItem listItems = collListItems[i]; 

string title1 = (string)collListItems[i]["Title"]; 
string summary1 = (string)collListItems[i]["Summary"]; 

//generating the table rows and cell is not currenlty working    
tr.Cells.Add(tc); 
tc.Controls.Add(new LiteralControl(title1)); 

//placeholders are no longer required, a table with rows and cells generated by the 
//for loop is required instead 
//plhPubTitleHyper.Controls.Add(new LiteralControl(title1)); 
//plhPubSummary.Controls.Add(new LiteralControl(summary1)); 

} 

} 

すべてのヘルプ:次のコードは、私がcurrenltyコードbeheindにテーブルを作成するという点で持っていますが、非常に私は現在、リスト項目を表示する必要がforループにそれを配線する方法を考え出したhavent WAHT示し大変感謝します。事前

のおかげで更新されたコード:

//creating a table to hold the list items in the for loop. This will replace the use 
//of the placeholders 
Table table1 = new Table(); 
TableRow tr = null; 

//for loop to iterate through the list retrieving the required amount of lits items 
//the number of times to iteratie through the loop will be specified by the user using 
//the custom property 
for (int i = 0; i < listItemsNumber && i < collListItems.Count; i++) 
{ 
SPListItem listItems = collListItems[i]; 

string title1 = (string)collListItems[i]["Title"]; 
//string summary1 = (string)collListItems[i]["Summary"]; 


tr = new TableRow(); 

TableCell c1 = new TableCell(); 
TableCell c2 = new TableCell(); 

c1.Controls.Add(new LiteralControl(title1)); 
//c2.Controls.Add(new LiteralControl(summary1));//This needs to be in another row 

tr.Cells.Add(c1); 
//tr.Cells.Add(c2); 

table1.Rows.Add(tr); 

//plhPubTitleHyper.Controls.Add(new LiteralControl(table1)); 

//plhPubTitleHyper.Controls.Add(new LiteralControl(title1)); 
//plhPubSummary.Controls.Add(new LiteralControl(summary1)); 

} 

plhPubTitleHyper.Controls.Add(table1); 
} 

更新されたコードは動作しますが、私は今、別の行ではなく、同じ行の列に行を追加し、要約を持ってできるようにする必要があります。

答えて

1
Table table1 = new Table(); 
    TableRow tr = null; 

    for (int i = 0; i < 5; i++) { 
     tr = new TableRow(); 

     //You can also do a loop for the cell 
     TableCell c1 = new TableCell(); 
     TableCell c2 = new TableCell(); 
     TableCell c3 = new TableCell(); 

     c1.Text = "Cell One"; 
     c2.Text = "Cell Two"; 
     c3.Text = "Cell Three"; 

     tr.Cells.Add(c1); 
     tr.Cells.Add(c2); 
     tr.Cells.Add(c3); 

     table1.Rows.Add(tr); 
    } 

    form1.Controls.Add(table1); 
関連する問題