56

How do I loop through data in WebForms like I do in ASP.NET MVC? For instance, in MVC, this is as simple as:

<table>
    @foreach (var myItem in g)
    { 
        @<tr><td>@MyItem.title<td></tr>
    }
</table>

What would the code behind look like?

Or, can I add an MVC project to a WebForms application so that I can use MVC functionality, instead?

TylerH
  • 20,799
  • 66
  • 75
  • 101
user1477388
  • 20,790
  • 32
  • 144
  • 264

3 Answers3

92

Rather than use a repeater, you can just loop through the list in a similar MVC type way using the <% %> and <%= %> tags.

<table>
  <% foreach (var myItem in g) { %>
    <tr><td><%= myItem.title %></td></tr>
  <% } %>
</table>

As long as the property you're looping through is acessible from the aspx/ascx page (e.g. declared as protected or public) you can loop through it. There is no other code in the code behind necessary.

<% %> will evaluate the code and <%= %> will output the result.

Here is the most basic example:

Declare this list at your class level in your code behind:

public List<string> Sites = new List<string> { "StackOverflow", "Super User", "Meta SO" };

That's just a simple list of strings, so then in your aspx file

<% foreach (var site in Sites) { %> <!-- loop through the list -->
  <div>
    <%= site %> <!-- write out the name of the site -->
  </div>
<% } %> <!--End the for loop -->
Brandon
  • 68,708
  • 30
  • 194
  • 223
  • Brandon, can you show me the code behind for something like this? Thanks. – user1477388 Feb 06 '13 at 15:43
  • @user1477388, there is no codebehind. As long as your list property is accessible to the front end (e.g. public or protected in the code behind), then you can loop through it. Similar to setting a model property in MVC. – Brandon Feb 06 '13 at 15:44
  • 4
    @user1477388 I know that answers what you were looking for, but that isn't really how you do it in webforms. You'd use a repeater or equivalent, and bind the data in the code-behind. – MikeSmithDev Feb 06 '13 at 21:34
  • We cannot show data on button click directly or handle some other events. – Kurkula Nov 04 '16 at 22:23
  • @MikeSmithDev What advantages does the repeater have over this? – Josh Noe Jan 17 '17 at 20:07
  • 2
    @JoshNoe it could make it a bit easier for formatting (with the Template options for Alternate items, Header, Footer, etc) as well as an OnItemDataBound event to do more complex data manipulation. That being said, I prefer an approach such as this answer (when appropriate) and to steer clear of web forms (and related controls) as much as I can. – MikeSmithDev Jan 17 '17 at 21:06
  • 1
    @Brandon: How do I handle Datasource and Databind of code behind when I use loop instead of repeater? Once I removed repeater and used foreach loop, my repeaterid in code behind is underlined in red, saying `The name xyz doesn't exits in current context.` – toofaced Jun 02 '17 at 16:10
  • how to implement this with link stylesheet tags within the aspx source page. Fx. within the <% foreach(var ref in Refs) %> – chri3g91 Dec 20 '18 at 13:44
  • @chri3g91, it's no different than the example, you're just missing the braces for the foreach statement. – Brandon Dec 20 '18 at 16:37
  • @toofaced I know this was a long time ago, but after I built the project it got rid of context error. – Mitch Wilkins Jan 18 '19 at 16:17
14

In WebForm you can use Repeater control:

<asp:Repeater id="cdcatalog" runat="server">
   <ItemTemplate>
       <td><%# Eval("title")%></td>
   </ItemTemplate>
</asp:Repeater>

In code behind:

cdcatalog.DataSource = yourData;
cdcatalog.DataBind();
phnkha
  • 7,782
  • 2
  • 24
  • 31
2

You can use a Repeater with any sort of valid DataSource (SqlDataSource, EntityDataSource, ObjectDataSource) object:

  1. Define the DataSource
  2. Reference the DataSource in your Reperater

....

 <asp:Repeater id="someRep" runat="server" DataSourceID="YourDataSource">
       <ItemTemplate>
          <tr>
                <td><%# Eval("PropertyName") %></td> 
          </tr>
    </ItemTemplate>
    </asp:Repeater>

...

Mithrandir
  • 24,869
  • 6
  • 50
  • 66