foreach循环来检索列表项并创建一个表 [英] foreach loop to retrieve list items and create a table

查看:161
本文介绍了foreach循环来检索列表项并创建一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我现在所拥有的(Webpart):

/ p>

 < table width =100%> 
< tr>
< td width =50%>
< asp:PlaceHolder ID =PlaceHolder1runat =server>< / asp:PlaceHolder>
< / td>
< td width =50%>
< asp:PlaceHolder ID =PlaceHolder2runat =server>< / asp:PlaceHolder>
< / td>
< / tr>
< / table>

后面的代码如下:

  foreach(SPListItem oListItem in listItemCollection)
{
awardYear = oListItem [Year]。ToString();
awardCategory = oListItem [Category]。ToString();
awardOrganiser = oListItem [Organizer]。ToString();
// awardNomWon = oListItem [NominatedWon]。ToString(); //不需要
//需要显示如何显示图像:
// awardLogo = oListItem [Logo]。ToString();

// string mydatetime;
// mydatetime = new DatTime(awardYear).ToString;

// DateTime convertedaValue = DateTime.Parse(awardYear);
// string awardYearConverted = convertedaValue.ToString();

PlaceHolder1.Controls.Add(
LiteralControl(awardYear ++|+< br />+
awardCategory ++| +< br />+
awardOrganiser ++|+< br />< br />));

PlaceHolder2.Controls.Add(
LiteralControl(awardYear ++|+< br />+
awardCategory ++| +< br />+
awardOrganiser ++|+< br />< br />));
$ b $ * b $ b PlaceHolder2.Controls.Add(
LiteralControl(< table>< tr>< td>+ awardYear +< / td> +
< td>&#160;< / td>+< td>+
awardCategory +< br />+
awardOrganiser +< / td>< / tr>));
* /

}

是从奖项列表中写出详细信息,并将其显示在两列中。目前一切正常,但问题是数据在表格的colum1和column2中重复。理想情况下,我希望foreach将创建表并填充列表中的细节。



可能有一些我错过了,但我可以'真的看到可能是什么。任何建议将不胜感激。

解决方案

在循环的表的哪一边你想要的信息,所以我建议你的循环

  int modCounter = 0; 
foreach(listListItemCollection中的SPListItem oListItem)
{
modCounter + = 1;
awardYear = oListItem [Year]。ToString();
awardCategory = oListItem [Category]。ToString();
awardOrganiser = oListItem [Organizer]。ToString();

if(modCounter%2 == 0)//如果modCounter可以被2整除(即偶数)
{
//添加第一列中的信息
}
else
{
//添加第二列中的信息
}
}


I am a little confused as to how to display list data in two columns in a webpart.

Here is what i have at the moment (Webpart):

<table width="100%">
  <tr>
    <td width="50%">
      <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </td>
    <td width="50%">
      <asp:PlaceHolder ID="PlaceHolder2" runat="server"></asp:PlaceHolder>
    </td>
  </tr>
</table>

and the code behind for it is the following:

foreach (SPListItem oListItem in listItemCollection)
{
    awardYear = oListItem["Year"].ToString();
    awardCategory = oListItem["Category"].ToString();
    awardOrganiser = oListItem["Organiser"].ToString();
    //awardNomWon = oListItem["NominatedWon"].ToString(); //not yet needed
    //need to fingure out how to display images:
    //awardLogo = oListItem["Logo"].ToString();

    //string mydatetime;
    //mydatetime = new DatTime(awardYear).ToString;

    //DateTime convertedaValue = DateTime.Parse(awardYear);
    //string awardYearConverted = convertedaValue.ToString();

    PlaceHolder1.Controls.Add(
        new LiteralControl(awardYear + " " + "|" + "<br/> " + 
                           awardCategory + " " + "|" + "<br/> " + 
                           awardOrganiser + " " + "|" + "<br/><br/>"));

    PlaceHolder2.Controls.Add(
        new LiteralControl(awardYear + " " + "|" + "<br/> " + 
                           awardCategory + " " + "|" + "<br/> " + 
                           awardOrganiser + " " + "|" + "<br/><br/>"));

    /*
    PlaceHolder2.Controls.Add(
        new LiteralControl("<table><tr><td>" + awardYear + "</td>" +
                           "<td>&#160;</td>" + "<td>" + 
                           awardCategory + "<br/>" +  
                           awardOrganiser + "</td></tr>" ));
    */

}

What I am trying to achieve is to write the details from an "Awards" list and display them in two columns. Currently everything works, but the problem is that data is being repeated in colum1 and column2 of the table. Ideally I was hoping that the "foreach" will create the table and populate it with the details from the list.

There maybe something that I have missed, but I can't really see what that could be. Any suggestions will be much appreciated.

解决方案

There's nothing to describe in the loop which side of the table you want the information in, so I'd suggest this with your loop

int modCounter = 0;
foreach (SPListItem oListItem in listItemCollection) 
{ 
    modCounter += 1;
    awardYear = oListItem["Year"].ToString(); 
    awardCategory = oListItem["Category"].ToString(); 
    awardOrganiser = oListItem["Organiser"].ToString(); 

    if(modCounter % 2 == 0) // If modCounter is divisible by 2 (ie even)
    {
         // Add information that goes in first column
    }
    else
    {
         // Add information that goes in second column
    }
} 

这篇关于foreach循环来检索列表项并创建一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆