Skip to content Skip to sidebar Skip to footer

Fill A Table From A List C# Asp.net

I have a class Car, with some atributtes Model and Year. I have a list (List carList = new List()) of some objects of the class Car, and I want to put this li

Solution 1:

If you are set on using a <table> and not any of the <asp:*> controls, you can manually build your markup on the page.

First, you need to be able to access the object from your markup. By making it a protected (or public) property on the code-behind, the page can access it:

protected IList<Car> CarList { get { return carList; } }

Then in the page (which inherits from the code-behind class) you can access that property to build markup. Something like this:

<% foreach (var car in CarList) { %>

<% } %>

Inside of that loop would be your rows, outside would be your table dressing. Something like this, perhaps:

<table><tr><th>Column Heading</th></tr>
<% foreach (var car in CarList) { %>
  <tr><td><%= car.SomeValue %></td></tr>
<% } %>
</table>

This is similar to how one may construct markup in MVC, so you can think of it like that. Though it's not common for WebForms and it's often recommended to use the framework the way it's meant to be used. In the long run, if you're set on using WebForms, it may be less work to adjust the template to work with the server-side controls than to manually build the markup. The server-side controls have the added benefit of lots of event binding and code-behind interactions.

Post a Comment for "Fill A Table From A List C# Asp.net"