C# 如果中继器控件中没有数据,如何在其内部显示消息?
我正在开发一个intranet web应用程序。我现在正在制作用户档案,其中显示了员工个人信息、培训课程、公司短期测验以及他提交的想法和建议的四个表格 我现在想要的是,如果员工没有任何建议,则在表中显示消息,例如(您没有任何建议),而不是在不告诉用户他没有建议的情况下显示带有标题的表那么怎么做呢? 我的ASP.NET代码:C# 如果中继器控件中没有数据,如何在其内部显示消息?,c#,asp.net,C#,Asp.net,我正在开发一个intranet web应用程序。我现在正在制作用户档案,其中显示了员工个人信息、培训课程、公司短期测验以及他提交的想法和建议的四个表格 我现在想要的是,如果员工没有任何建议,则在表中显示消息,例如(您没有任何建议),而不是在不告诉用户他没有建议的情况下显示带有标题的表那么怎么做呢? 我的ASP.NET代码: <asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource4">
<asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource4">
<HeaderTemplate>
<div>
<table border="1">
<thead>
<tr>
<td colspan="3">
<center> <strong>Safety Suggestions</strong> </center>
</td>
</tr>
<tr>
<td>
<center> <strong>Suggestion Title</strong> </center>
</td>
<td>
<center> <strong>Description</strong> </center>
</td>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<p>
<%# Eval("Title") %>
</p>
</td>
<td>
<p>
<%# Eval("Description") %>
</p>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</div>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsLog.Username
FROM dbo.SafetySuggestionsLog INNER JOIN
dbo.employee ON dbo.SafetySuggestionsLog.Username = dbo.employee.Username
WHERE (dbo.employee.Username = @Username)">
<SelectParameters>
<asp:Parameter Name="Username" />
</SelectParameters>
</asp:SqlDataSource>
安全建议
建议标题
说明
您可以使用页脚模板来管理按摩,如下所示
第一步
<FooterTemplate>
<%-- Label used for showing Error Message --%>
<asp:Label ID="lblErrorMsg" runat="server" Text="Sorry, no item is there to show." Visible="false">
</asp:Label>
</FooterTemplate>
1。您可以检查行数据绑定事件中的中继器项计数
2.在中继器中的某个位置放置标签(页脚等)
3.如果计数小于1(在页脚行找到标签)
4.用“无数据显示”填充标签
查看此问题的答案:可能重复感谢。非常感谢您的帮助。什么是rptDemo和repeaterTopItems?rptDemo是通过数据绑定触发此事件的中继器。repeaterTopItems实际上是一种类型,应该是:rptDemo,因为它指的是同一个中继器。
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Repeater rptDemo = sender as Repeater; // Get the Repeater control object.
// If the Repeater contains no data.
if (repeaterTopItems != null && repeaterTopItems.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Footer)
{
// Show the Error Label (if no data is present).
Label lblErrorMsg = e.Item.FindControl("lblErrorMsg") as Label;
if (lblErrorMsg != null)
{
lblErrorMsg.Visible = true;
}
}
}
}
1. You can check for repeater items count in row databound event
2. Place a label somewhere in your repeater(footer etc.)
3. If count < 1 (find your label on footer row)
4. Populate label with "No data to display"