Default text for empty Repeater control

前端 未结 8 968
醉梦人生
醉梦人生 2021-02-02 05:43

Using VS 2008, I have a Repeater control:



        
相关标签:
8条回答
  • 2021-02-02 06:13

    Even better: this subclass adds an EmptyDataTemplate property. In your markup, put in an element just as you would an element. By default this will hide the header and footer if there's no data; you can change this with the HeaderVisibleWhenEmpty and FooterVisibleWhenEmpty properties in markup.

    public class RepeaterWithEmptyDataTemplate : Repeater
    {
        public virtual ITemplate EmptyDataTemplate { get; set; }
    
        protected virtual bool DefaultHeaderVisibleWhenEmpty
        {
            get { return false; }
        }
    
        protected virtual bool DefaultFooterVisibleWhenEmpty
        {
            get { return false; }
        }
    
        public bool HeaderVisibleWhenEmpty
        {
            get { return ViewState["hve"] == null ? DefaultHeaderVisibleWhenEmpty : (bool) ViewState["hve"]; }
            set { ViewState["hve"] = value; }
        }
    
        public bool FooterVisibleWhenEmpty
        {
            get { return ViewState["fve"] == null ? DefaultFooterVisibleWhenEmpty : (bool) ViewState["fve"]; }
            set { ViewState["fve"] = value; }
        }
    
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            if (Items.Count == 0 && EmptyDataTemplate != null)
            {
                var emptyPlaceHolder = new PlaceHolder {ID = "empty", Visible = true};
                EmptyDataTemplate.InstantiateIn(emptyPlaceHolder);
    
                //figure out where to put placeholder
                RepeaterItem footer =
                    Controls.OfType<RepeaterItem>().FirstOrDefault(i => i.ItemType == ListItemType.Footer);
                if (footer == null)
                {
                    //add to end if no footer
                    Controls.Add(emptyPlaceHolder);
                }
                else
                {
                    Controls.AddAt(Controls.IndexOf(footer), emptyPlaceHolder);
                }
    
                //hide header and footer according to properties.
                foreach (RepeaterItem x in Controls.OfType<RepeaterItem>())
                {
                    switch (x.ItemType)
                    {
                        case ListItemType.Header:
                            x.Visible = HeaderVisibleWhenEmpty;
                            break;
                        case ListItemType.Footer:
                            x.Visible = FooterVisibleWhenEmpty;
                            break;
                    }
                }
            }
        }
    }
    

    Sample in markup:

    <myprefix:RepeaterWithEmptyDataTemplate runat=server>
        <ItemTemplate>blah blah blah</ItemTemplate>
        <EmptyDataTemplate>Hey, no data!</EmptyDataTemplate>
    </myprefix:RepeaterWithEmptyDataTemplate>  
    

    Please note that the DataBind method must be called, or the emptydatatemplate won't be displayed.

    0 讨论(0)
  • 2021-02-02 06:19

    Joaos answer can even be simplified. In the footer, do not set the visible-property of your default item to false, but use the following expression:

    <FooterTemplate>
        <asp:Label ID="defaultItem" runat="server" 
            Visible='<%# YourRepeater.Items.Count == 0 %>' Text="No items found" />
    </FooterTemplate>
    

    This way, you can save the code behind.

    0 讨论(0)
  • 2021-02-02 06:24

    On the basis of answer n. 3 i adopted the following solution which seems good enough to me:

    <asp:Literal ID="emptyDataRowCnt" runat="server" Visible='<%# MyRepeater.Items.Count == 0 %>'>
        <li class="row emptyDataRow">No data here</li>
    </asp:Literal>
    
    0 讨论(0)
  • 2021-02-02 06:25

    Another possibility:

    <FooterTemplate>
        <asp:Label ID="lblEmptyData" runat="server" Visible='<%# ((Repeater)Container.NamingContainer).Items.Count == 0 %>' Text="No items found" />
    </FooterTemplate>
    

    The benefit of this code snippet is that you aren't dependent on the ID you assigned to your repeater.

    0 讨论(0)
  • 2021-02-02 06:28

    Using the visible properties and asp literals provided in previous answers, I extended erionpc's answer to either display a 'no data' or record count.

    <FooterTemplate>
                    <asp:Literal ID="repeaterEmptyDataRow" runat="server" Visible='<%# Results_Repeater.Items.Count == 0 %>'>
                        <tr>
                            <td>No Data Found</td>    
                        </tr>
                    </asp:Literal>
                    <asp:Literal ID="repeaterResultsDataRow1" runat="server" Visible='<%# Results_Repeater.Items.Count != 0 %>'>
                        <tr>
                            <td>
                    </asp:Literal>
                    <asp:Literal ID="repeaterResultsDataRow2" runat="server" Visible='<%# Results_Repeater.Items.Count != 0 %>' Text='<%# String.Concat(Results_Repeater.Items.Count.ToString(), " records.") %>' />
                    <asp:Literal ID="repeaterResultsDataRow3" runat="server" Visible='<%# Results_Repeater.Items.Count != 0 %>'>
                            </td>
                        </tr>
                    </asp:Literal>
                    </table>
                </FooterTemplate>
    

    I don't write a lot of asp, so there maybe a cleaner way to do this.

    0 讨论(0)
  • 2021-02-02 06:30

    The best way I found to solve this :

    1. Add the following label anywhere on your page -

      <asp:Label ID="lblEmptyRepeater" runat="server" Visible="false" Text="There are no items in this repeater"></asp:Label>
      
    2. Add the OnPreRenderEvent for your Repeater

      <asp:Repeater ID="emptyRepeater" runat="server" OnPreRender="emptyRepeater_PreRender">
      
    3. Now inside this event in your codebehind, write the code

      protected void emptyRepeater_PreRender(object sender, EventArgs e)
      {
         lblEmptyRepeater.Visible = (emptyRepeater.Items.Count==0);
      }
      
    4. Now your empty repeater should be checked after the data is bound but before render on page, and show the label if it is empty.

    0 讨论(0)
提交回复
热议问题