使用VS2008,我有一个Repeater控件:
<asp:Repeater runat="server" ID="storesRep" DataSourceID="storeSqlDataSource"
OnItemDataBound="StoresRep_ItemDataBound">
<ItemTemplate>
<table style="padding:0px">
<tr>
<td style="width:200px"><asp:Label ID="infoLbl" runat="server">
Choose stores for upload:</asp:Label>
</td>
<td style="width:110px">
<asp:Label ID="storeLbl" runat="server" Text='<%# Bind("Name") %>'>
</asp:Label>
</td>
<td><asp:CheckBox runat="server" ID="storeCheck" /></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="storeSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:someConnectionString %>"
SelectCommand="SELECT [StoreId], [Name] FROM [Store] Order By [Name]">
</asp:SqlDataSource>现在,如果数据源没有从数据库返回任何项目,我想显示一个默认文本,如"No stores“。到目前为止,我主要使用GridView,但由于EmptyDataText属性,我没有遇到任何问题。
发布于 2011-03-11 20:14:39
您可以解决以下问题:Repeater不支持一种内置方式来完成您正在执行的操作,方法是将FooterTemplate与OnItemDataBound事件结合使用,并仅在数据源未返回任何项时显示页脚。
有关如何执行此操作的示例,可在以下位置找到:
Handling Empty Data in an ASP.NET Repeater control
How to show Empty Template in ASP.NET Repeater control?
发布于 2012-03-28 19:42:44
Joaos answer甚至可以简化。在页脚中,不要将默认项目的visible-property设置为false,而应使用以下表达式:
<FooterTemplate>
<asp:Label ID="defaultItem" runat="server"
Visible='<%# YourRepeater.Items.Count == 0 %>' Text="No items found" />
</FooterTemplate>这样,您就可以保存后面的代码。
发布于 2015-02-18 21:32:28
另一种可能性是:
<FooterTemplate>
<asp:Label ID="lblEmptyData" runat="server" Visible='<%# ((Repeater)Container.NamingContainer).Items.Count == 0 %>' Text="No items found" />
</FooterTemplate>此代码片段的好处是您不依赖于分配给中继器的ID。
https://stackoverflow.com/questions/5271500
复制相似问题