Reputation: 542
I need to hide table rows in a DataList if column data returns null from SQL Server (for each individual column). I have it working successfully but this method will be very tedious as I have about 100 rows in my table. Surely there is a simpler way.
Here is my C# code:
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:
<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>
Upvotes: 0
Views: 725
Reputation: 126
just modify this code by adding multiple conditions
string value = Convert.ToString( row["MyColumn"]);
if (string.IsNullOrEmpty(value))
Upvotes: 0
Reputation: 35544
You can wrap the ItemTemplate contents with a PlaceHolder and use a Ternary Operator to set the visibility.
<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
Upvotes: 1