0

我正在使用 VS2010,我有一个带有 ListView 的 aspx 页面。现在我想隐藏具有 ID 的 ListView 列。

我已经厌倦了这段代码:

ListView1.Columns[0].Width = 0;

但它不会让我,它在'columns'部分给出一个错误,说“ system.web.ui.webcontrols.listview不包含'columns'的定义并且没有扩展方法'columns'接受第一个参数找不到类型“system.web.ui.webcontrols.listview”(您是否缺少 using 指令或程序集引用?) “。

我也试过这段代码:

ListView1.FindControl("idTh").Visible = false;
         foreach (ListViewItem item in ListView1.Items)
        {
            item.FindControl("idCol").Visible = false;
        } 

但它给了我一个“未设置为引用的对象”错误。

我究竟做错了什么??

这是我的 aspx 代码:

<asp:ListView ID="ListView1" runat="server" DataSourceID="Paysment_Dates" 
        InsertItemPosition="LastItem" 
        onselectedindexchanged="ListView1_SelectedIndexChanged">
        <AlternatingItemTemplate>
            <tr class="t_even">
                <td>
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                </td>
                <td >
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishYearLabel" runat="server" 
                        Text='<%# Eval("PublishYear") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishMonthLabel" runat="server" 
                        Text='<%# Eval("PublishMonth") %>' />
                </td>
                <td>
                    <asp:Label ID="PayDteLabel" runat="server" Text='<%# Eval("PayDte") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishDteLabel" runat="server" 
                        Text='<%# Eval("PublishDte") %>' />
                </td>
            </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                        Text="Update" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Cancel" />
                </td>
                <td>
                    <asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("ID") %>'  />
                </td>
                <td>
                    <asp:TextBox ID="PublishYearTextBox" runat="server" 
                        Text='<%# Bind("PublishYear") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishMonthTextBox" runat="server" 
                        Text='<%# Bind("PublishMonth") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PayDteTextBox" runat="server" Text='<%# Bind("PayDte") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishDteTextBox" runat="server" 
                        Text='<%# Bind("PublishDte") %>' />
                </td>
            </tr>
        </EditItemTemplate>
        <EmptyDataTemplate>
            <table runat="server" style="">
                <tr>
                    <td>
                        No data was returned.</td>
                </tr>
            </table>
        </EmptyDataTemplate>
        <InsertItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                        Text="Insert" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Clear" />
                </td>
                <td>
                    <asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("ID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishYearTextBox" runat="server" 
                        Text='<%# Bind("PublishYear") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishMonthTextBox" runat="server" 
                        Text='<%# Bind("PublishMonth") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PayDteTextBox" runat="server" Text='<%# Bind("PayDte") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishDteTextBox" runat="server" 
                        Text='<%# Bind("PublishDte") %>' />
                </td>
            </tr>
        </InsertItemTemplate>
        <ItemTemplate>
            <tr class="t_odd">
                <td>
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                </td>
                <td id="idCol" runat="server">
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishYearLabel" runat="server" 
                        Text='<%# Eval("PublishYear") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishMonthLabel" runat="server" 
                        Text='<%# Eval("PublishMonth") %>' />
                </td>
                <td>
                    <asp:Label ID="PayDteLabel" runat="server" Text='<%# Eval("PayDte") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishDteLabel" runat="server" 
                        Text='<%# Eval("PublishDte") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                            <tr runat="server" style="">
                                <th runat="server">
                                </th>
                                <th id="idTh" runat="server">
                                    ID</th>
                                <th runat="server">
                                    Publish Year</th>
                                <th runat="server">
                                    Publish Month</th>
                                <th runat="server">
                                    Pay Date</th>
                                <th runat="server">
                                    Publish Date</th>
                            </tr>
                            <tr ID="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" style="">
                        <asp:DataPager ID="DataPager1" runat="server">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                    ShowLastPageButton="True" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <SelectedItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                </td>
                <td>
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishYearLabel" runat="server" 
                        Text='<%# Eval("PublishYear") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishMonthLabel" runat="server" 
                        Text='<%# Eval("PublishMonth") %>' />
                </td>
                <td>
                    <asp:Label ID="PayDteLabel" runat="server" Text='<%# Eval("PayDte") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishDteLabel" runat="server" 
                        Text='<%# Eval("PublishDte") %>' />
                </td>
            </tr>
        </SelectedItemTemplate>
    </asp:ListView>
4

1 回答 1

1

ListViews 没有列,GridViews 有。您必须在要隐藏的 ListView 中有一些其他控件。请张贴您的aspx代码。

编辑:(答案)

FindControl() 只查看控件的直接子代,而不是孙代等。

您将需要创建一个循环遍历所有子项以找到所需内容的方法。

例如:

public Control MyFindControl(Control parent, string controlIdToFind)
{
    foreach(Control c in parent.Controls)
    {
        Control found = MyFindControl(c, controlIdToFind);
        if (found != null)
        {
           return found;
        }
    }

    // control not found.
    return null;
}

然后将您的代码更改为:

    ListView1.FindControl("idTh").Visible = false;
    foreach (ListViewItem item in ListView1.Items)
    {
        MyFindControl(item, "idCol").Visible = false;
    } 

如果我的 C# 生锈了,请原谅我。

于 2013-10-11T08:09:34.320 回答