2011-05-18 51 views
3

我需要隐藏asp:repeater中的一列。最好通过CSS将它们隐藏在服务器端而不仅仅是HTML。该中继器有一个ID,但我很难找到它在调试器中拥有的表。考虑到中继器的工作原理,我不确定它甚至有可能。我给HTML tableID并将其设置为runat="server",但它与错误炸毁了文件的是否可以在asp:repeater中隐藏一列?

意外结束寻找 标签。

我该怎么办?我需要切换到一个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> 
+0

你是什么意思“隐藏一列”?中继器没有列,它只是为每个绑定的项目发射内容。 – 2011-05-18 22:05:50

+0

如果你想使用Repeater作为表格,你应该考虑使用GridView。 – 2011-05-18 22:47:56

+0

@Joel C对,这就是我所说的“考虑中继器的工作原理”。对不起,我不是更清楚。 @Tim Schmelter - 我同意。直放站看起来不太合适。我将切换到一个gridview。如果您想将您的评论移至答案,我会将其标记为答案。 – 2011-05-19 12:32:46

回答

4

遵循Tim Schmelter的建议,我切换到了gridview。这样我可以使用

gridviewObj.Columns[index].Visible = false; 

,从而避免中继隐藏多个<td>

1

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

e.Row.FindControl("ControlID"); 
+0

没有真正的单列来处理。该表具有标题,交替模板和项目模板来处理。我要切换到一个gridview。 – 2011-05-19 12:34:17

+0

@ P.Brian.Mackey:如果你想使用[AlternatingItemTemplate](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.aspx),你可以使用它中继器控制。如果你想编辑更新删除类型的功能,那么你应该使用GridView,但如果你只想显示数据,那么你可以使用Repeater。无论如何,选择是你的。 – jams 2011-05-19 12:45:15

+0

我在这种情况下喜欢gridview的原因是因为我可以做'gridviewObj.Columns [5] .Visible = false;'。我不能用中继器很容易地做到这一点。即使我使用'Row.FindControl',也没有单一的控件可以找到。我需要识别几个'​​'并隐藏每一个。 – 2011-05-19 13:06:54

8

我们可以隐藏使用中继器的HTML表的列ItemDataBound事件

要做到这一点,我们指定的ID被隐藏表格单元格并标记细胞为RUNAT =“服务器”

<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 

在上面的代码表中的第一列“COL1”被设置为被隐藏

-1

隐藏在中继器控制以下列方式柱:

的ItemDataBound即使直放站的使用下面的代码

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

在收割者表格单元格内前帕德应该如下

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

还通过提供标题单元格id & Runat在HTML和visible = false在页面加载。

您应使用以下Usings:

using System.Web.UI.HtmlControls; 

这样,我已经能够在Repeater控件隐藏列。