【问题标题】:ASP.NET Hiding a <asp:Panel>/<th>/<tr>/<td> inside a ListViewASP.NET 在 ListView 中隐藏 <asp:Panel>/<th>/<tr>/<td>
【发布时间】:2011-03-02 18:34:00
【问题描述】:
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="itemHolder" 
            OnItemCommand="listViewCmd" OnItemDeleting="OnItemDeleting" 
            OnItemEditing="OnItemEditing">
            <LayoutTemplate>
                <table class = "tblItemDetail" style = "color:Black;" width="100%" border="0" cellpadding="5">
                    <tr>
                        <asp:Panel ID="pnlNameHead" runat="server">
                            <th id="thName" runat="server">Name
                            </th>   
                        </asp:Panel>
                        <th>Address
                        </th>
                        <th>Contact No.
                        </th>
                        <th>E-mail Address
                        </th>
                        <th>Edit
                        </th>
                        <th>Delete
                        </th>
                    </tr>
                    <asp:PlaceHolder ID="itemHolder" runat="server"></asp:PlaceHolder>
                    </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <asp:Panel ID="pnlName" runat="server">
                        <td align="center" id="tdName" runat="server">
                            <asp:Literal ID="lit1" runat="server" Text='<%# Eval("SupplierName") %>'></asp:Literal>
                        </td>
                    </asp:Panel>
                    <td align="center">
                    <asp:Literal ID="Literal1" runat="server" Text='<%# Eval("SupplierAdd") %>'></asp:Literal>
                    </td>
                    <td align="center">
                    <asp:Literal ID="Literal2" runat="server" Text='<%# Eval("SupplierContact") %>'></asp:Literal>
                    </td>
                    <td align="center">
                    <asp:Literal ID="Literal3" runat="server" Text='<%# Eval("SupplierEmail") %>'></asp:Literal>
                    </td>
                    <td align="center">
                    <asp:Button ID="Button3" runat="server" Text="Edit"  CommandArgument='<%# DataBinder.Eval(Container.DataItem, "SupplierID") %>' CommandName="edit" />
                    </td>
                    <td align="center">
                    <asp:Button ID="Button2" runat="server" Text="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "SupplierID") %>' CommandName="delete" />
                    </td>                   
                </tr>

            </ItemTemplate> 

        </asp:ListView> 

我得到了我的 aspx Web 表单的源代码。我正在尝试隐藏供应商名称的列。我已经尝试了很多方法,例如:

  • ListView1.FindControl("pnlNameHead").Visible = false
  • 面板 pnlName = (Panel)ListView.FindControl("pnlName");
    • pnlName.Visible = 假;

但我总是得到“对象引用未设置为对象的实例”。任何人都可以帮助我吗?这几天我一直在尝试解决这个问题。

【问题讨论】:

  • 隐藏 thName (?)
  • 在声明中尝试

标签: c# javascript .net asp.net html


【解决方案1】:

不要将 Control 的 Visible 属性设置为 false 与使其隐藏混淆。 (是的,我知道这听起来如何,但请耐心等待)。

如果将控件的 Visible 属性设置为 false,这意味着当页面的 html 在服务器上呈现时,控件将不会生成任何要包含的标记。请参阅 MSDN 上的 Control.Visible。这可能是您想要的,在这种情况下,安德鲁查尔顿的答案就是您所需要的。

但是,如果您希望 &lt;th&gt; 和关联的 &lt;td&gt; 元素出现在浏览器页面的标记中,但不可见(即隐藏),那么您需要使用 CSS 使用 @987654325 设置它们的样式@例如

<LayoutTemplate>
    <table class = "tblItemDetail" style = "color:Black;" width="100%" border="0" cellpadding="5">
        <tr>
            <asp:Panel ID="pnlNameHead" runat="server">
                <th id="thName" runat="server" style="display:none;">Name</th>   
             </asp:Panel>
             ...
        </tr>
</LayoutTemplate>
<ItemTemplate>
    <tr>
        <asp:Panel ID="pnlName" runat="server">
            <td align="center" id="tdName" runat="server" style="display:none;">
                <asp:Literal ID="lit1" runat="server" Text='<%# Eval("SupplierName") %>'></asp:Literal>
            </td>
        </asp:Panel>

【讨论】:

  • Phil,您提供的信息非常有用。抱歉,但我忘了提到我正在尝试隐藏代码隐藏中的 listview 列。我的问题是如何在不出现 NullReference 异常的情况下隐藏它?
  • 例如。单击某个按钮后,我希望这些列不可见
【解决方案2】:

您无法访问和修改 ItemTemplate 中的内容 (AFAIK),因为它们还不是实际控件;模板只是描述每个项目将包含的内容。创建后,您可以使用 ItemDataBound 事件来遍历并隐藏每个 Item 中的面板。像这样的:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if(e.Item.ItemType == ListViewItemType.DataItem)
    {
        Panel pnl = (Panel)e.Item.FindControl("pnlName");
        pnl.Visible = false;
    }
}

唯一的其他选择是动态构建所有模板或使用用户控件:

Dynamically change GridView item template

编辑:刚刚看到您在表格单元格周围设置了面板。您可以删除面板并直接在 ItemDataBound 中访问单元格:

HtmlControl td = (HtmlControl)e.Item.FindControl("tdName");
td.Visible = false;

【讨论】:

  • 感谢您的回答。但是在尝试了您的建议后,我仍然得到“对象引用未设置为对象实例”
  • 查看我上次的编辑,忘记添加对 ItemType 的检查。有不同类型的项目同时被绑定,但只有某些项目会包含您想要的控件。这类似于 GridViews,您必须确保您位于 DataRow 而不是 HeaderRow 或 FooterRow。
  • 另外,将该面板放在 之外会生成无效的 XHTML。
【解决方案3】:

以下是问题的一些可能原因。

1) 您只能在创建控件后进行更改,通常在 ItemDataBoundLayoutCreated 事件中进行。也许您想这样做太早了?

2) 没有将表格单元格包装在另一个控件中的有效概念。我有点惊讶这不会阻塞解析器,但也许它可以工作,因为它只是 HTML。如果您尝试使用&lt;asp:Table&gt; 执行此操作,它将无法正常工作。无论如何,它不会呈现有效的 HTML - Panel 创建一个 div 所以表格元素将被包装在一个 div 中。无论如何,没有理由这样做。相反,只需寻找元素本身。在您的示例中,thtd 元素已设置为 runat="server",因此请直接搜索它们。

3) FindControl 不是递归的。尝试使用递归实现。这是我使用的扩展方法(最后一个参数只是为了让我可以与 FindControl 共享一个名称,您可以将名称更改为其他名称,如果您愿意,可以将其删除)。这可能是 FindControl 没有返回任何内容的原因,因为您要查找的控件将位于另一个控件 (tr) 的 Controls 集合中。

    public static Control FindControl(this Control baseControl, string id, bool recurse) 
    {
        foreach (Control ctl in baseControl.Controls)
        {
            if (ctl.ID==id)
            {
                return (ctl);
            }
            if (recurse && ctl.Controls.Count > 0)
            {
                Control subCtl = ctl.FindControl(id,recurse);
                if (subCtl != null)
                {
                    return (subCtl);
                }
            }
        }
        return (null);
    }

一般来说,虽然这是可行的,而且我之前也尝试过,但以编程方式更改布局模板是一件麻烦事,可能会导致代码中出现大量条件处理。如果由于某种原因你不能只使用 CSS,我会制作另一个模板。

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签