ASP.NET 2.0 Resources

Powered by Blogger

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.

5 Comments:

  • How about this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    repeater.DataBind()
    Dim record As String = repeater.Items.Count.ToString
    If record = 0 Then
    repeater.Visible = False
    Else
    repeater.Visible = True
    End If
    End Sub

    By Blogger karulann, at 9:20 AM  

  • In regards to the simple fix of setting the visibible property of the repeater, it doesn't seem to work for me, it is always hidden...

    I have assumed that the ID for that repeater should be Repeater1.

    Am I missing something?

    By Blogger ces4848, at 4:05 PM  

  • as for me, the solution from Nicholas is the best, my version is:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    using System.ComponentModel;
    using System.ComponentModel.Design;


    namespace CubManagement.Controls
    {
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:SmartRepeater runat=\"server\">CLOSING TAG HERE")]
    public class SmartRepeater : Repeater
    {
    private ITemplate emptyTemplate;

    [Category("Data")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [Editor(typeof(Repeater), typeof(System.Drawing.Design.UITypeEditor))]
    public ITemplate EmptyTemplate
    {
    get { return this.emptyTemplate; }
    set { this.emptyTemplate = value; }
    }

    protected override void OnDataBinding(EventArgs e)
    {
    base.OnDataBinding(e);
    if (this.Items.Count == 0)
    {
    this.Controls.Clear();
    EmptyTemplate.InstantiateIn(this);
    }
    }
    }
    }

    By Blogger ifesdjeen, at 12:24 PM  

  • Hi all,

    for me, don't work if use in UpdatePanel (Ajax) .

    Any solution ?? thanks.

    By Blogger Kiquenet, at 9:00 AM  

  • Students are now moving to programming and language. They are working on different programs and wnt matlab programming help little bit. So we should do for them.

    By Blogger aliyaa, at 6:53 PM  

Post a Comment

<< Home

Created dolly