Search code examples
c#asp.netlistcode-behind

How do you iterate through a list that is set in code behind?


I need to loop through a list on an aspx webpage. It's part of an object that is populated in the code behind. All of the other properties of the object are strings and I can access them like so:

<td align="center"><%# Eval("PartNumber") %></td>

or

<td align="center"><%# Eval("ContractNumber") %></td>

I was trying to do something like the following with the list:

<% List<PeriodData> periodValues = (List<PeriodData>)Eval("PeriodValues");
   foreach (var period in periodValues)
   {
       Response.Write("<td align='center'>");
       period.ETC.ToString("#,0.#");
       Response.Write("</td>");
   }
%>

I get this error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Is there a way to accomplish this?


Solution

  • I ended up creating the entire report table in the code behind using a StringBuilder and setting a label's text field to the built up string. Not the most elegant solution and not how I wanted to handle it, but, hey, it works.