1

こんにちは私はコードビハインドでテーブルを作成し、「アワード」リストのリストアイテムの値を2つの列のテーブルに表示するWebパーツを持っています。このテーブルでは、各アワードが2列に並んで表示されます。foreachループを使用して、すべてのリストアイテムの値を取得し、同時にテーブルを生成しています。

問題は、何らかの理由ですべてのリストアイテム値が表示されていないことです。また、リストアイテムの値が出力で繰り返されています。以下は私が現在持っているコードの抜粋です:

using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{

try
{

SPList awardsList = web.Lists["Awards"];

SPListItemCollection listItemCollection = awardsList.Items;

int modCounter = 0;
foreach (SPListItem oListItem in listItemCollection)
{
modCounter += 1;

awardYear = oListItem["Year"].ToString();
awardCategory = oListItem["Category"].ToString();
awardNomWon = oListItem["NominatedWon"].ToString();
awardLogo = oListItem["Logo"].ToString(); //need to fingure out how to display images

Table tbl = new Table();
TableRow tblRow = new TableRow();
TableCell tblCellLeft = new TableCell(); //for the awards in the first column
TableCell tblCellRight = new TableCell(); //for the awards in the second column

tblCellLeft.VerticalAlign = VerticalAlign.Top;
tblCellLeft.HorizontalAlign = HorizontalAlign.Center;
tblCellLeft.CssClass = ("style5");

tblCellRight.VerticalAlign = VerticalAlign.Top;
tblCellRight.HorizontalAlign = HorizontalAlign.Center;


//values for the left column
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear +
"</div>"));

tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory + 
"</div>"));

tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon + 
"</div>"));

//Values for the right column
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear + 
"</div>"));

tblCellRight.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory 
+ "</div>"));

tblCellRight.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon + 
"</div>"));

tblCellRight.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" + 
awardLogo.Replace(",", "") + "</div>"));

//adding Rows
tblRow.Cells.Add(tblCellLeft);
tblRow.Cells.Add(tblCellRight);
tbl.Rows.Add(tblRow);

if (modCounter % 2 == 0)
{                            
//values from tblCellLeft
PlaceHolder6.Controls.Add(tbl);
}
else
{
//values from the tblRightCell
PlaceHolder2.Controls.Add(tbl);
}

}

}
catch (Exception err)
{
PlaceHolder3.Controls.Add(new LiteralControl(err.ToString()));
}

}
}


});

何が問題なのかわかりません。どんな援助も大歓迎です。

ありがとう

4

2 に答える 2

1

次のように代わりに for ループを使用して、これを解決することができました。

//getting the awards list and extracting correct values from the necesary fields
//asp running with elevated privilegs
SPSecurity.RunWithElevatedPrivileges(delegate()
{

using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{



try
{

SPList awardsList = web.Lists["Awards"];

SPListItemCollection listItemCollection = awardsList.Items;


//getting all list items and displaying them next to each other row by row


//int modCounter = 0;

//Creating the table
Table tbl = new Table();

//foreach (SPListItem oListItem in listItemCollection)
int x = listItemCollectionI.Count;
for(int i = 0; (i * 2) < x; i++) // divide total item collection by two, each loop 
iteration, add two awards to a row (left cell, right cell)
{
// get listItemCollection[i];
//Create table rows, table cells

int leftIndexer = i * 2;
int rightIndexer = (i * 2) + 1;

if (leftIndexer == x)
{
break;
}                                

TableRow tblRow = new TableRow();
TableCell tblCellLeft = new TableCell(); //for the awards in the first column
TableCell tblCellRight = new TableCell(); //for the awards in the second column

tblCellLeft.VerticalAlign = VerticalAlign.Top;
tblCellLeft.HorizontalAlign = HorizontalAlign.Center;
tblCellLeft.CssClass = ("style5");

tblCellRight.VerticalAlign = VerticalAlign.Top;
tblCellRight.HorizontalAlign = HorizontalAlign.Center;


// get the values
awardYear = listItemCollection[leftIndexer]["Title"].ToString();
awardCategory = listItemCollection[leftIndexer]["Category"].ToString();
awardNomWon = listItemCollection[leftIndexer]["NominatedWon"].ToString();
if(listItemCollection[leftIndexer]["Logo"] != null)
awardLogo = (string)listItemCollection[leftIndexer]["Logo"];

// add to left cell
//values for the left column
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear + 
"</div>"));
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory + 
"</div>"));
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon + 
"</div>"));
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" + 
awardLogo.Replace(",", "") + "</div>"));

// add left cell to row
tblRow.Cells.Add(tblCellLeft);

if (rightIndexer < x) // if this item exists in the collection (prevent bug with odd 
number of awards)
{


// get the values
awardYear = listItemCollection[rightIndexer]["Title"].ToString();
awardCategory = listItemCollection[rightIndexer]["Category"].ToString();
awardNomWon = listItemCollection[rightIndexer]["NominatedWon"].ToString();

if (listItemCollection[rightIndexer]["Logo"] != null)
    awardLogo = (string)listItemCollection[rightIndexer]["Logo"];


// add to right cell
//Values for the right column
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear + 
"</div>"));
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory 
+ "</div>"));
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon + 
"</div>"));
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" + 
awardLogo.Replace(",", "") + "</div>"));

//add right cell to row
tblRow.Cells.Add(tblCellRight);

}

// add row to table
tbl.Rows.Add(tblRow);

}

PlaceHolder6.Controls.Add(tbl); // add table outside of loop 
}
catch (Exception err)
{
PlaceHolder3.Controls.Add(new LiteralControl(err.ToString()));
}

}
}



});

ただし、これにより別の問題が発生します。返された値が適切な順序でソートされていません。つまり、何らかの理由で、最新のリスト項目が一番上にあるはずの場所に一番下にプッシュされています。他の値は正しい順序で表示されます。

これを適切な順序でソートするための提案はありますか? それは非常に高く評価されます。

ありがとう、

于 2012-01-17T10:39:16.517 に答える
0

すべての結果を 1 つのテーブルにまとめたいと思いますか?

その場合、 new Table onceのみを宣言していることを確認する必要があります。これを行うには、ループを開始する前にテーブルを宣言します。foreach

Table tbl = new Table(); 
foreach (SPListItem oListItem in listItemCollection) 
{
   // get data
   // create cells & rows
   // insert data into cells, cells into rows, and rows into table
}
于 2012-01-16T19:09:18.130 に答える