2011-05-18 52 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控件隱藏列。