在Visual Studio 2010 C#中隐藏ListView列 [英] Hide ListView column in visual studio 2010 C#

查看:106
本文介绍了在Visual Studio 2010 C#中隐藏ListView列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Am using VS2010, I have an aspx page that has a ListView. Now I want to hide the ListView column that has the ID.

我对这段代码感到厌倦:

I have tired this code:

ListView1.Columns[0].Width = 0;

但是它不允许我这样做,它在列"部分给出了一个错误,说" system.web.ui.webcontrols.listview不包含列"的定义,也没有扩展方法列" '找不到接受类型为'system.web.ui.webcontrols.listview'的第一个参数(您是否缺少using指令或程序集引用?)".

But it won't let me, it gives an error on the 'columns' part saying "system.web.ui.webcontrols.listview does not contain a definition for 'columns' and no extension method 'columns' accepting a first argument of type 'system.web.ui.webcontrols.listview' could not be found (are you missing a using directive or an assembly reference?)".

我也尝试了以下代码:

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

但是它给我一个"对象未设置为引用"的错误.

But it gives me an "Object not set to reference" error.

我在做什么错??

这是我的aspx代码:

This is my aspx code:

<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>

推荐答案

ListViews没有列,而GridViews没有列.您必须在ListView中具有要隐藏的其他控件.请发布您的aspx代码.

ListViews don't have columns, GridViews do. You must have some other control in your ListView that you want to hide. Please post your aspx code.

(答案)

FindControl()仅查看控件的直接子级,而不查看子级等.

FindControl() only looks at the immediate children of the control, not the grandchildren, etc.

您将需要创建一个遍历所有子级的方法以找到所需的内容.

You will need to create a method that loops through all children to find what you want.

例如:

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#生锈,请原谅我.

Forgive me if my C# is rusty.

这篇关于在Visual Studio 2010 C#中隐藏ListView列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆