有谁知道在asp.net listview中隐藏列的方法?

Pau*_*hin 12 asp.net listview

我知道您可以在ItemTemplate中放置<%if%>语句来隐藏控件但列仍然存在.您不能将<%%>语句放入LayoutTemplate,这是声明列标题的位置,因此出现问题.有谁知道更好的方法?

Dea*_*lin 19

这是我刚才做的另一个解决方案,看到我明白你想做什么:

这是你的ASCX/ASPX

    <asp:ListView ID="ListView1" runat="server" DataSourceID="MyDataSource" ItemPlaceholderID="itemPlaceHolder" OnDataBound="ListView1_DataBound">
        <LayoutTemplate>
            <table border="1">
                <tr>
                    <td>Name</td>
                    <td>Age</td>
                    <td runat="server" id="tdIsSuperCool">IsSuperCool</td>
                </tr>
                <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td><%# Eval("Name") %></td>
                <td><%# Eval("Age") %></td>
                <td runat="server" id="myCol" visible='<%# (bool)Eval("IsSuperCool") %>'>true</td>
            </tr>
        </ItemTemplate>
    </asp:ListView>
    <asp:ObjectDataSource 
        ID="MyDataSource" 
        runat="server" 
        DataObjectTypeName="BusinessLogicLayer.Thing" 
        SelectMethod="SelectThings"
        TypeName="BusinessLogicLayer.MyObjectDataSource" />
Run Code Online (Sandbox Code Playgroud)

这是背后的代码

/// <summary>
/// Handles the DataBound event of the ListView1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void ListView1_DataBound(object sender, EventArgs e)
{
    ListView1.FindControl("tdIsSuperCool").Visible = false;
}
Run Code Online (Sandbox Code Playgroud)

在数据绑定中做任何你想做的事.因为该列现在是runat服务器,并且您正在处理控件的DataBound,所以当您执行ListView1.FindControl("tdIsSuperCool")时,您处于布局模板中,因此其工作方式类似于冠军.

把你想要的任何业务逻辑控制到td的可见性,你就是好的.