當Repeater
不包含任何項目時,它根本不會呈現爲HTML格式,即使是HeaderTemplate
或FooterTemplate
。我需要在客戶端操作它,即使它是空的。渲染空中繼器
有沒有什麼辦法總是渲染HTML中的Repeater?
當Repeater
不包含任何項目時,它根本不會呈現爲HTML格式,即使是HeaderTemplate
或FooterTemplate
。我需要在客戶端操作它,即使它是空的。渲染空中繼器
有沒有什麼辦法總是渲染HTML中的Repeater?
在<FooterTemplate>
中,添加一個標籤,其中包含一些空的數據文本並將其visible屬性設置爲false。
<FooterTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblEmptyData"
Text="No Data To Display" runat="server" Visible="false">
</asp:Label>
</td>
</tr>
</table>
</FooterTemplate>
現在檢查綁定中繼器的數據,如果沒有行返回,然後使標籤可見,否則不行。
更多詳細信息here。
試試這個
protected bool IsDataEmpty
{
get
{
ICollection list = Repeater1.DataSource as ICollection;
return list.Count == 0 ? true : false;
}
}
在標記:
<table width="80%">
<tr runat="server"
visible='<%# IsDataEmpty %>'>
<td>
There is no data to display
</td>
</tr>
一步一步按照鏈接:Link
爲@Saurabh說,使用<FooterTemplate>
添加一個Label與指定您的留言在Text屬性中,並將其可見屬性設置爲false,如下所示:
<FooterTemplate>
<%-- Label used for showing Error Message --%>
<asp:Label ID="ErrorMessage" runat="server" Text="Sorry!!" Visible="false">
</asp:Label>
</FooterTemplate>
然後在代碼隱藏中使用以下邏輯;如果沒有數據,顯示信息,否則,顯示數據如下:
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;
}
}
}
}
這實際上是更好,更完整的答案 – BritishDeveloper
<asp:Repeater ID="rptList" runat="server" DataSourceID="odsList">
...
<FooterTemplate>
<%if (rptList.Items.Count == 0)
{ %>
**Your message**
<%} %>
</FooterTemplate>
</asp:Repeater>
我認爲回傳時除非您再次調用DataBind(),否則回傳時「rptList.Items」爲空。所以有一個警告 –
'HeaderTemplate'和'即使數據源沒有返回項目FooterTemplate'仍應被渲染。這些模板是否爲空? –