Render empty repeater

你离开我真会死。 提交于 2019-12-06 22:51:28

问题


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?


回答1:


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.




回答2:


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;
            }
        }
    }
}



回答3:


<asp:Repeater ID="rptList" runat="server" DataSourceID="odsList">  
    ...
    <FooterTemplate>  
        <%if (rptList.Items.Count == 0)  
          { %>  
          **Your message**  
        <%} %>  
    </FooterTemplate>  
</asp:Repeater>



回答4:


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



来源:https://stackoverflow.com/questions/6579814/render-empty-repeater

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!