使用 LayoutTemplate 您可以定义列,然后使用 OnLayoutCreated 事件隐藏特定列。
<asp:ListView ID="ListView1" class="lv" runat="server" DataSourceID="SqlDataSource1"
onprerender="ListView1_PreRender"
onselectedindexchanged="ListView1_SelectedIndexChanged"
style="text-align: left" OnLayoutCreated="OnLayoutCreated">
<LayoutTemplate>
<table id="itemPlaceholderContainer">
<tr>
<td><asp:Label id="columnToHide" runat="server" Text="Column 1" /></td>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("name")></td>
</tr>
</ItemTemplate>
然后在代码隐藏中:
protected void OnLayoutCreated(object sender, EventArgs e)
{
this.ListView1.FindControl("columnToHide").Visible = false;
}