-1

I have the following asp:Repeater setup:

<asp:Repeater runat="server" id="ArticleInfoRepeater">
    <ItemTemplate>
        <div class="col-md-9" style="margin-top: 30px;">
            <h2><%#: Eval("Title") %></h2>
            <div style="margin-top:10px;">
                <%#: Eval("Content") %>
            </div>
        </div>
    </ItemTemplate>
 </asp:Repeater>

and I want to populate it using a List<> that is returned when an item from a select list is selected.

My problem is, how do I set the Eval value. I have seen this question Understanding ASP.NET Eval() and Bind() but is it really the case that I have to create a whole new class just to set these 2 simple values? I know that is a fairly simple task, but it seems rather drastic just to set 2 variables.

If not, how do I set these 2 values? The list I am trying to use to populate the fields has properties corresponding the name of the eval field.

I assume after that I will use a for loop to populate them? But I guess I need a command to then make it populate the field, or will the assignment of the Eval value automatically populate the field each time?

There is really no clear instructions on how to achieve my goal, which I think is a fairly simple and straight forward goal.

Community
  • 1
  • 1
Alex
  • 3,730
  • 9
  • 43
  • 94

1 Answers1

1

What you would want to do, would be to build a model of your data.

public class Example
{
     public int Id { get; set; }
     public string Title { get; set; }
     public string Content { get; set; }
}

Now you have your model, then you would call your database. To populate your List<Example>, which it will return for you. Then once you have your List<Example> you would simply do:

List<Example> content = PopulateListExample();
ArticleInfoRepeater.DataSource = content;
ArticleInfoRepeater.DataBind();

Then on your front end you would reference your properties, so <%# Eval("Title") %> and it will reference the your Enumerable source.

Greg
  • 11,302
  • 2
  • 48
  • 79