foreach loop to retrieve list items and create a table

半城伤御伤魂 提交于 2019-12-13 03:22:12

问题


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.


回答1:


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
    }
} 



回答2:


Building your table using something like the Table control would be much neater and maintainable.

<asp:Table ID="MyTable" runat="server" />

Then in your code build up the data using TableRow and TableCell objects.




回答3:


Use Rows Property of System.Web.UI.WebControls.Table. It means remove your html markup and use Table web control instead.



来源:https://stackoverflow.com/questions/8850062/foreach-loop-to-retrieve-list-items-and-create-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!