codingjoe
codingjoe

Reputation: 810

ASP.Net repeaters bind with a collection of objects

I have a collection List<Person> Persons, and in a Person there is a Country (Country.Name). I have successfully bound my repeater to my collection, however trying write out the country name is giving me some troubles, also I have a a bool IsActive in my Persons, which I would like to show an icon instead of true/false.

protected void Page_Load(object sender, EventArgs e)
    {
        List<Person> persons = GetPersons();
        RP_Persons.DataSource = persons;
        RP_Persons.DataBind();
    }

    protected void RP_Persons_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
           // How do I get my stuff here? I need to get the country name and be able to use a <div class="active"><img src='xxxx' /></div>
        }
    }

Here is my markup:

<ItemTemplate>
    <tr>
        <td><%# DataBinder.Eval(Container.DataItem, "Email") %></td>
        <td><%# DataBinder.Eval(Container.DataItem, "Registered") %></td>
        <td><%# DataBinder.Eval(Container.DataItem, "Country") %></td>
        <td><%# DataBinder.Eval(Container.DataItem, "IsActive") %></td>
    <tr>
</ItemTemplate>

Upvotes: 2

Views: 9188

Answers (2)

Shai Cohen
Shai Cohen

Reputation: 6249

Although the method you provide in your answer does work, IMHO this type of code is easier to accomplish and maintain in the code-behind, as you have suggested.

Here is how you would do that:

This is the code for the aspx file:

<ItemTemplate>
    <tr>
        <td><%# DataBinder.Eval(Container.DataItem, "Email") %></td>
        <td><%# DataBinder.Eval(Container.DataItem, "Registered") %></td>
        <td><asp:Label id="lblCountry" runat="server" /></td>
        <td>
            <asp:Image id="imgActive" ImageUrl="~/IMAGE_NAME" runat="server" />
            <asp:Image id="imgInactive" ImageUrl="~/IMAGE_NAME" runat="server" />
        </td>
    <tr>
</ItemTemplate>

Code for the aspx.cs file:

protected void RP_Persons_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
           Person person = (Person)e.Item.DataItem;
           ((Label)e.Item.FindControl("lblCountry")).Text = person.Country.Name;
           if(person.IsActive) {
              ((Image)e.Item.FindControl("imgActive")).Visible = True;
              ((Image)e.Item.FindControl("imgInactive")).Visible = False;
           }
           else {
              ((Image)e.Item.FindControl("imgActive")).Visible = False;
              ((Image)e.Item.FindControl("imgInactive")).Visible = True;
           }
        }
    }

Upvotes: 3

codingjoe
codingjoe

Reputation: 810

As simple as it was:

<td><%# DataBinder.Eval(Container.DataItem, "Country.Name") %></td>

Now I just need to figure out how to handle the boolean value and show an image instead of true/false.

(edit) ....and I found a way to toggle between a true/false image. I find it rather clumpsy, so I am open for improvements.

<%# DataBinder.Eval(Container.DataItem, "IsActive").ToString() == "True" ? "<span class='label label-success'>Active</span>" : "<span class='label'>Inactive</span>"%>

Upvotes: 1

Related Questions