ASP.NET 2.0 Resources

Powered by Blogger

.NET 2.0 Repeater - Different template for first item

I've got just another little trick with repeater control for you. Have you ever needed more complex rules for rendering ASP.NET 2.0 Repeater items not just ItemTemplate and AlternatingItemTemplate templates? For example different style/layout for the first item?

There is one really simple solution how to declaratively achieve such behavior. Simply define multiple placeholders and show/hide them according to data Container's ItemIndex property. The following example shows how to check for first repeater item...

   1:  <asp:Repeater runat="server">
   2:      <ItemTemplate>
   3:          <asp:PlaceHolder runat="server" Visible="<%# (Container.ItemIndex == 0) %>" >
   4:          </asp:PlaceHolder>
   5:      </ItemTemplate>
   6:  </asp:Repeater>

Read more > Posted on: 7/25/2006 08:58:00 AM

Repeater control templates with empty datasource

Just a quick note and quick fix. I have a Repeater control with both Header, Item and Footer templates in one of my ASP.NET 2.0 projects. This control is rendering contextual site navitagion to ul/li structure. Sometimes this navigation is simply empty and i needed some easy way (preferably declarative) to hide Header and Footer templates when the datasource is empty because they were still beeing rendered.

To my knowledge, there is no property on Repeater control that i could for such behaviour so i solved my problem with this little trick. I'm using the Visible property of Repeater control and hiding it when there are no data in the datasource available.

   1:  <asp:Repeater 
   2:      runat="server" 
   3:      DataSourceID="ContextualSitemapDataSource" 
   4:      Visible="<%# Repeater1.Items.Count > 0 %>" 
   5:      EnableViewState="false">
   6:      <HeaderTemplate>
   7:          <h2>Contextual Links</h2>
   8:          <ul class="menublock">
   9:       </HeaderTemplate>
  10:      <ItemTemplate>
  11:          <li><a 
  12:              runat="server" 
  13:              href='<%# Eval("[navigateurl]") %>'><%# Eval("Title") %></a>
  14:          </li>
  15:      </ItemTemplate>
  16:      <FooterTemplate>
  17:          </ul>
  18:      </FooterTemplate>
  19:  </asp:Repeater>

Is there some straight way how to do this declaratively in ASP.NET 2.0? I tried google search but didn't find anything usefull.

Read more > Posted on: 7/21/2006 07:49:00 PM

Created dolly