[英]How to hide table rows in a DataList if column data returns null from SQL Server
I need to hide table rows in a DataList if column data returns null from SQL Server (for each individual column). 如果列数据从SQL Server返回(对于每个单独的列),则需要在DataList中隐藏表行。 I have it working successfully but this method will be very tedious as I have about 100 rows in my table. 我已经成功运行了该方法,但是此方法将非常繁琐,因为我的表中大约有100行。 Surely there is a simpler way. 当然,有一种更简单的方法。
Here is my C# code: 这是我的C#代码:
protected void DataList1_ItemDataBound1(object sender, DataListItemEventArgs e)
{
if ((String.IsNullOrEmpty(((Label)e.Item.FindControl("lblAccountStatus")).Text)))
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("rowAccountStatus");
row.Visible = false;
}
if ((String.IsNullOrEmpty(((Label)e.Item.FindControl("lblAccountName")).Text)))
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("rowAccountName");
row.Visible = false;
}
}
Here is my webform markup: 这是我的Webform标记:
<asp:DataList ID="DataListAccount" runat="server" OnItemDataBound="DataList1_ItemDataBound1">
<ItemTemplate>
<tr>
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountStatus" runat="server" Text='<%# Eval("ACCOUNT_STATUS") %>'></asp:Label>
</td>
</tr>
<tr id="rowAccountName">
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountName" runat="server" Text='<%# Eval("ACCOUNT_NAME") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
You can wrap the ItemTemplate contents with a PlaceHolder and use a Ternary Operator to set the visibility. 您可以使用PlaceHolder包装ItemTemplate内容,并使用三元运算符设置可见性。
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# !string.IsNullOrEmpty(Eval("ACCOUNT_STATUS").ToString()) ? true : false %>'>
<tr>
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountStatus" runat="server" Text='<%# Eval("ACCOUNT_STATUS") %>'></asp:Label>
</td>
</tr>
</asp:PlaceHolder>
But I would recommend you make sure you filter the source data of empty items. 但我建议您确保过滤空项目的源数据。 Something like 就像是
SELECT * FROM accounts WHERE account_status IS NOT NULL
just modify this code by adding multiple conditions 只需通过添加多个条件来修改此代码
string value = Convert.ToString( row["MyColumn"]);
if (string.IsNullOrEmpty(value))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.