I have below list view , how can i hide column by code behind ?
<asp:ListView ID="AuditLogListView" runat="server" OnItemCreated="AuditLogListView_ItemCreated">
<LayoutTemplate>
<table class="table table-striped table-bordered small">
<tr class="table-secondary">
<th id="BlockHeader" runat="server" style="white-space: normal;">
<asp:Literal ID="BlockHeaderLiteral" runat="server" Text="<%$ Resources:AppResources, AuditInformationBlockHeader %>" />
</th>
</tr>
<asp:PlaceHolder runat="server" ID="ItemPlaceholder"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td id="BlockStatus" runat="server">
<%# Eval("BlockStatus")%>
</td>
</tr>
</ItemTemplate>
After binding data with list view i tried below code behind but with this header text only hide but column still can still visible
if (groupOrBlockValue == 'W')
{
AuditLogListView.FindControl("BlockHeader").Visible = false;
AuditLogListView.FindControl("BlockHeaderLiteral").Visible = false;
//AuditLogListView.FindControl("BlockStatus").Visible = false;
}
2
Answers
At which event have you written this code?
Required this code in the OnDataBound event. And in your code this event missing.
<asp:ListView ID="AuditLogListView" runat="server" OnItemCreated="AuditLogListView_ItemCreated" OnDataBound="AuditLogListView_DataBound">
<asp:Literal ID="BlockHeaderLiteral" runat="server" Text="<%$ Resources:AppResources, AuditInformationBlockHeader %>" /><asp:PlaceHolder runat="server" ID="ItemPlaceholder"></asp:PlaceHolder>
C# Code:
protected void AuditLogListView_DataBound(object sender, EventArgs e)
{AuditLogListView.FindControl("BlockHeaderLiteral").Visible = false;}
Missing part of the list view?
With this:
So note in the layout, we added a "id" = LabW – that lets you hide the header.
so, a simple button that would toggle (hide/show) the lvColum, then this works:
So, we get this:
And clicking on the button, we get this:
And both the values changed – they persist – even when you click again (to toggle and show the hidden columns).
Edit: =====================================================
So, say this markup:
And code behind button to hide is this: