title: How do I vary my ItemTemplate inside an asp:Repeater?
body: <p>I have a user control which is used to display search results. The HTML for each result displayed will vary based on the type of result being displayed: "contacts" are displayed in one way, "news articles" are displayed in another, etc. There are around 10 different types of results that are all marked up differently when they get to HTML &mdash; so I need around 10 or so different templates for individual results that I can choose between based on the current item being displayed.</p>
  
  <p>I'm using an <code>asp:Repeater</code> to display the results, but I don't know how to select the appropriate template within the <code>asp:Repeater &lt;ItemTemplate&gt;</code>. Ideally I'd like the ASP to select the appropriate template to use based upon the object type being passed in via the <code>searchResultsRepeater.DataSource</code> &mdash; but unfortunately I can't use switch on type (see <a href="http://blogs.msdn.com/b/peterhal/archive/2005/07/05/435760.aspx" rel="nofollow noreferrer">this blog entry for C# switch on type</a>). I can however just pass through an enum value for the type of result being displayed.</p>
  
  <p>In the backend C# code I have an abstract inline <code>SearchResult</code> class, and children of that class like <code>ContactSearchResult</code>, <code>NewsArticleSearchResult</code>, etc. The <code>searchResultsRepeater.DataSource</code> would then be bound to a <code>List&lt;SearchResult&gt;</code>. Each <code>SearchResult</code> contains a <code>ResultListingType type</code> field which gives the type of the listing to be displayed.</p>
  
  <h2>Attempt 1: using control flow inside the ASP itself</h2>
  
  <p>My first attempt was something like this:</p>
  
  <pre><code>        &lt;asp:Repeater ID="searchResultsRepeater" runat="server"&gt;
          &lt;ItemTemplate&gt;
          &lt;div class="item"&gt;
  
          &lt;% switch (DataBinder.Eval(Container.DataItem, "type")) { %&gt;
  
          &lt;% case ResultListingType.CONTACT: %&gt;
  
              &lt;p&gt;&lt;%# DataBinder.Eval(Container.DataItem, "firstName") %&gt;&lt;/p&gt;
              &lt;p&gt;&lt;%# DataBinder.Eval(Container.DataItem, "lastName") %&gt;&lt;/p&gt;
  
              &lt;% break; %&gt;
  
          &lt;% case ResultListingType.NEWS: %&gt;
  
              &lt;p&gt;&lt;%# DataBinder.Eval(Container.DataItem, "newsHeadline") %&gt;&lt;/p&gt;
              &lt;p&gt;&lt;%# DataBinder.Eval(Container.DataItem, "newsDate") %&gt;&lt;/p&gt;
  
              &lt;% break; %&gt;
  
          &lt;% Case AnotherTypeOfListing1: %&gt;
          &lt;% Case AnotherTypeOfListing2: %&gt;
          &lt;% Case AnotherTypeOfListing3: %&gt;
          &lt;% Case AnotherTypeOfListing4: %&gt;
          &lt;% Case AnotherTypeOfListing5: %&gt;
          &lt;% etc... %&gt;
  
          &lt;% } %&gt;
  
          &lt;/div&gt;
  
          &lt;/ItemTemplate&gt;
          &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>Unfortunately, this doesn't work:</p>
  
  <ul>
  <li>"switch" and "if" both give "invalid expression term" inside the <code>&lt;%# ... %&gt;</code> brackets.</li>
  <li>"Container.DataItem" gives "the name "Container" does not exist in the current context" inside <code>&lt;% ... %&gt;</code> brackets.</li>
  </ul>
  
  <h2>Attempt 2: setting asp:PlaceHolder's to Visible = False</h2>
  
  <p>I found something that looked useful at <a href="https://stackoverflow.com/q/342330/633626">how to change the ItemTemplate used in an asp:repeater?</a>. I then tried something like:</p>
  
  <pre><code>        &lt;asp:Repeater ID="searchResultsRepeater" runat="server"&gt;
          &lt;ItemTemplate&gt;
          &lt;div class="item"&gt;
  
          &lt;asp:PlaceHolder ID="newsResultListing" runat="server"&gt;
              &lt;p&gt;&lt;%# DataBinder.Eval(Container.DataItem, "newsHeadline") %&gt;&lt;/p&gt;
              &lt;p&gt;&lt;%# DataBinder.Eval(Container.DataItem, "newsDate") %&gt;&lt;/p&gt;
          &lt;/asp:PlaceHolder&gt;
  
          &lt;asp:PlaceHolder ID="contactResultListing" runat="server"&gt;
              &lt;p&gt;&lt;%# DataBinder.Eval(Container.DataItem, "firstName") %&gt;&lt;/p&gt;
              &lt;p&gt;&lt;%# DataBinder.Eval(Container.DataItem, "lastName") %&gt;&lt;/p&gt;
          &lt;/asp:PlaceHolder&gt;
  
          &lt;/div&gt;
  
          &lt;/ItemTemplate&gt;
          &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>In my ItemDataBound event I did:</p>
  
  <pre><code>        Control newsResultListing = e.Item.FindControl("newsResultListing");
          newsResultListing.Visible = false;
          Control contactResultListing = e.Item.FindControl("contactResultListing");
          contactResultListing.Visible = false;
          switch (item.type)
          {
              case ResultListingType.CONTACT:
                  contactResultListing.Visible = true;
                  break;
              case ResultListingType.NEWS:
                  newsResultListing.Visible = true;
                  break;
              default:
                  throw new Exception("Unknown result listing type");
          }
  </code></pre>
  
  <p>Unfortunately this doesn't work because ASP seems to still be running the contents of the PlaceHolder even after I set <code>Visible = false</code>. I get the error "DataBinding: 'usercontrols_ResultsListing+ContactResultsListing' does not contain a property with the name 'newsHeadline'" &mdash; i.e. the <code>newsResultListing</code> PlaceHolder is still looking for the "newsHeadline" field, even though that field doesn't exist for the result listing type being displayed.</p>
  
  <p>In fact I've tried a quick test <code>throw new Exception("e");</code> in my ItemDataBound, and it looks like the "DataBinding" error is thrown even before control flow gets to the ItemDataBound method, so there's really nothing I can do in there to avoid this error.</p>
  
  <p>I suppose I could add every single field to the parent class and leave most of them null in my children, but that seems really ugly.</p>
  
  <hr/>
  
  <p>Is there a way to make this work, or an easier way to vary my ItemTemplate based upon the type of Container.DataItem I'm currently iterating over? I'm very new to ASP so there's likely something simple that I've missed. :)</p>