Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET
Print

Alternating Types in Bound Repeater

4.83/5 (3 votes)
15 Jul 2013CPOL 11.8K  
In this alternate version, we take into account the possibility that binding to a list of different types may cause compile errors.

Variation: Alternating Data Types

Suppose I wanted to expand the example further to support binding a list of different types of classes (say, Animal and Human). The binding code would get very complicated, as the bound contents inside of placeholders still get evaluated even when the placeholders are not visible.

To simplify the binding code, you can use a repeater to wrap the objects of different types (pay particular attention to the use of the ListWrap function):

<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">

  // Sample class we can use for binding.
  public class Animal
  {
    public Boolean CanFly { get; set; }
    public String Description { get; set; }
  }

  // Another sample class for binding.
  public class Human
  {
    public String Name { get; set; }
    public String Description { get; set; }
  }

  // Setup binding in page load.
  protected void Page_Load(Object sender, System.EventArgs e)
  {
    rpItems.DataSource = new List<Object> {
      new Animal  {CanFly = false, Description = "Duck"},
      new Animal  {CanFly = true, Description = "Duck"},
      new Animal  {CanFly = false, Description = "Duck"},
      new Animal  {CanFly = true, Description = "Goose!"},
      new Human  {Name = "Elephant Man", Description = "I am not an animal!"}};
    rpItems.DataBind();
  }

  // Helper function to wrap a single item in a list if it is of the specified type.
  protected List<Object> ListWrap<SomeType>(Object item)
  {
    List<Object> returnList = new List<object>();
    if (item is SomeType)
    {
      returnList.Add(item);
    }
    return returnList;
  }

</script>

<html>
<head>
    <title>Markup Conditions Using Bound Code Block</title>
</head>
<body>
    <form id="frmMain" runat="server">

      <!-- This is our main data bound control. -->
      <asp:Repeater runat="server" ID="rpItems">
        <HeaderTemplate>
          <ul>
        </HeaderTemplate>
        <ItemTemplate>
          <li>

            <%-- This repeater is bound to a list with one or zero animals. --%>
            <asp:Repeater runat="server" DataSource="<%# ListWrap<Animal>(Container.DataItem)%>">
              <ItemTemplate>

                <!--  We use placeholder visibility to use different markup depending on the bound value. -->
                <asp:PlaceHolder runat="server" Visible="<%# (Container.DataItem as Animal).CanFly%>">
                  <b>I can fly!</b>
                </asp:PlaceHolder>
                <asp:PlaceHolder runat="server" Visible="<%# !(Container.DataItem as Animal).CanFly %>">
                  I can't fly.
                </asp:PlaceHolder>
                <%# HttpUtility.HtmlEncode(((Animal)Container.DataItem).Description) %>

              </ItemTemplate>
            </asp:Repeater>

            <%-- This repeater is bound to a list with one or zero humans. --%>
            <asp:Repeater runat="server" DataSource="<%# ListWrap<Human>(Container.DataItem)%>">
              <ItemTemplate>
                <%# HttpUtility.HtmlEncode((Container.DataItem as Human).Name)%>:
                "<%# HttpUtility.HtmlEncode((Container.DataItem as Human).Description)%>"
              </ItemTemplate>
            </asp:Repeater>

          </li>
        </ItemTemplate>
        <FooterTemplate>
          </ul>
        </FooterTemplate>
      </asp:Repeater>

    </form>
</body>
</html>

When the item is of the incorrect type, the repeater gets bound to an empty list, and the binding code inside of a repeater bound to an empty collection does not get evaluated. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)