Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render empty repeater

When Repeater contains no items it's not get rendered in HTML at all, even HeaderTemplate or FooterTemplate. I need to manipulate it on client-side even if it's empty.

Is there any way to always render Repeater in HTML?

like image 637
Max Al Farakh Avatar asked Jul 05 '11 08:07

Max Al Farakh


4 Answers

In the <FooterTemplate>, add a Label with some empty data text and set its visible property to false.

<FooterTemplate>
<table>
 <tr>
 <td>
 <asp:Label ID="lblEmptyData"
        Text="No Data To Display" runat="server" Visible="false">
 </asp:Label>
 </td>
 </tr>
 </table>           
 </FooterTemplate>

Now check the the data while binding repeater, if no rows return then make label visible otherwise no action.

More details here.

like image 161
Saurabh Avatar answered Oct 31 '22 09:10

Saurabh


as @Saurabh said, use <FooterTemplate> add a Label with specifying your message in the Text property and set its visible property to false like this:

<FooterTemplate>
        <%-- Label used for showing Error Message --%>
        <asp:Label ID="ErrorMessage" runat="server" Text="Sorry!!" Visible="false">
        </asp:Label>
    </FooterTemplate>

Then in the code-behind use the following logic; if there is no data, show the message, otherwise, show the data as following:

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Repeater rpt = sender as Repeater; // Get the Repeater control object.

    // If the Repeater contains no data.
    if (rpt != null && rpt.Items.Count < 1)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            // Show the Error Label (if no data is present).
            Label ErrorMessage = e.Item.FindControl("ErrorMessage") as Label;
            if (ErrorMessage != null)
            {
                ErrorMessage.Visible = true;
            }
        }
    }
}
like image 8
Ali Ahmed Avatar answered Oct 31 '22 08:10

Ali Ahmed


<asp:Repeater ID="rptList" runat="server" DataSourceID="odsList">  
    ...
    <FooterTemplate>  
        <%if (rptList.Items.Count == 0)  
          { %>  
          **Your message**  
        <%} %>  
    </FooterTemplate>  
</asp:Repeater>
like image 4
alireza alimardan Avatar answered Oct 31 '22 07:10

alireza alimardan


Try this

protected bool IsDataEmpty     
  {    
       get    
       {    
           ICollection list = Repeater1.DataSource as ICollection;    
           return list.Count == 0 ? true : false;    
       }    
  }

In Markup :

<table width="80%">   
     <tr runat="server"

         visible='<%# IsDataEmpty %>'>    
         <td>    
             There is no data to display    
           </td>    
     </tr>

for step by step follow the link :Link

like image 1
SMK Avatar answered Oct 31 '22 08:10

SMK