是否可以隐藏asp:repeater中的列?

时间:2011-05-18 21:53:52

标签: asp.net

我需要隐藏asp:repeater中的列。最好是通过CSS隐藏服务器端,而不仅仅是HTML。转发器有ID,但我很难找到它在调试器中拥有的表。考虑到转发器如何工作,我不确定它是否可行。我向HTML table提供了一个ID,并将其设置为runat="server",但它出现了错误

  

意外的文件结尾寻找   标签

我该怎么办?我需要切换到gridview吗?使用gridview,我可以更容易地做到这一点。

<asp:repeater id="repeaterId" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td><%# DataBinder.Eval(Container.DataItem, "col1")%></td>
                            <td nowrap="nowrap" align="left"><%# DataBinder.Eval(Container.DataItem, "col2")%></font></td>                          
                        </tr>
                    </ItemTemplate>
                    <AlternatingItemTemplate>                                                   <tr>
                            <td><%# DataBinder.Eval(Container.DataItem, "col1")%></td>
                            <td nowrap="nowrap" align="left"><%# DataBinder.Eval(Container.DataItem, "col2")%></td> 
                    </AlternatingItemTemplate>
                    <HeaderTemplate>
                        <table id="rPhysicalTable" class="cssTable">
                            <tr class="aClass">
                                <th>col1</th>
                                <th>col2</th>   
                            </tr>
                    </HeaderTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:repeater>

4 个答案:

答案 0 :(得分:9)

我们可以使用转发器ItemDataBound事件隐藏html表的列

为此,我们指定要隐藏的表格单元格的id,并将单元格标记为runat =&#34; server&#34;

<asp:repeater id="repeaterId" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td id="tdhideItem" runat="server"><%# DataBinder.Eval(Container.DataItem, "col1")%></td>
                            <td nowrap="nowrap" align="left"><%# DataBinder.Eval(Container.DataItem, "col2")%></font></td>                          
                        </tr>
                    </ItemTemplate>
                    <AlternatingItemTemplate>                                                   <tr>
                            <td id="tdhideAltItem" runat="server"><%# DataBinder.Eval(Container.DataItem, "col1")%></td>
                            <td nowrap="nowrap" align="left"><%# DataBinder.Eval(Container.DataItem, "col2")%></td> 
                    </AlternatingItemTemplate>
                    <HeaderTemplate>
                        <table id="rPhysicalTable" class="cssTable">
                            <tr class="aClass">
                                <th id="thhideHeader" runat="server">col1</th>
                                <th>col2</th>   
                            </tr>
                    </HeaderTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:repeater>

以下vb.net代码被指定为

背后的代码
  Protected Sub repeaterId_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repeaterId.ItemDataBound

         If e.Item.ItemType = ListItemType.Item  Then
        DirectCast(e.Item.FindControl("tdhideItem"), HtmlTableCell).Visible = False    
        End If

         If e.Item.ItemType = ListItemType.AlternatingItem
        Then
        DirectCast(e.Item.FindControl("tdhideAltItem"), HtmlTableCell).Visible = False    
        End If

      If e.Item.ItemType = ListItemType.Header Then
        DirectCast(e.Item.FindControl("thhideHeader"), HtmlTableCell).Visible = False    
        End If
        End Sub

在上面的代码中,表格的第一列&#34; col1&#34;被设置为隐藏

答案 1 :(得分:4)

按照Tim Schmelter的建议,我改用了gridview。这样我可以使用

gridviewObj.Columns[index].Visible = false;

因此避免在转发器中隐藏多个<td>

答案 2 :(得分:1)

您可以在此事件中使用ItemDataBound事件Repeater,您可以根据自己的要求隐藏任何行或列。这是一个MSDN link
在这种情况下,您可以使用FindControl方法查找您的控件并将其Visible属性设置为false。

e.Row.FindControl("ControlID");

答案 3 :(得分:-1)

以下列方式隐藏转发器控件中的列:

ItemDataBound即使是重复使用以下代码

HtmlTableCell tdTableCell = (HtmlTableCell)e.Item.FindControl("tdTableCell");
tdTableCell.Visible = False;

在收割者表格单元格的前面部分应该如下

<td id="tdTableCell" runat="server"></td>

同样通过提供id&amp; amp来隐藏标题单元格。在html中运行,在页面加载中visible = false

您应该使用以下用途:

using System.Web.UI.HtmlControls;

这样我就可以在转发器控件中隐藏列。