P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

Is it possible to hide a column in an asp:repeater?

I need to hide a column in an asp:repeater. Preferably, hide them server side not just in HTML via CSS. The repeater has an ID, but I am having difficulty finding the table that it owns within the debugger. Considering how a repeater works I'm not sure its even possible. I gave the HTML table an ID and set it to runat="server", but it blew up with error

Unexpected end of file looking for tag.

How can I do it? Do I need to switch to a gridview? I could probably do this a lot easier with a 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>

Upvotes: 5

Views: 15351

Answers (4)

user2353540
user2353540

Reputation: 1

Hide a column in repeater control in the following way:

ItemDataBound Even of repearter use the following code

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

In the front pade inside the reaper table cell should be as follows

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

Also hide the header cell by providing it id & Runat in the html and visible = false in page load.

You should use the following Usings:

using System.Web.UI.HtmlControls;

This way I have been able to hide column in repeater control.

Upvotes: -1

Akhil
Akhil

Reputation: 471

We can hide the column of a html table using the repeaters ItemDataBound event

To do this, we specify the id for the table cells to be hidden and tag the cell as runat="server"

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

The following vb.net code is specified as code behind

  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

In the above code the first column of the table "col1" is set to be hidden

Upvotes: 12

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

Following Tim Schmelter's advice I switched to a gridview. This way I can use

gridviewObj.Columns[index].Visible = false;

And thus avoid hiding multiple <td> in a repeater.

Upvotes: 4

Amit
Amit

Reputation: 22076

You can use ItemDataBound event of Repeater in this event you can hide any row or column according to your requirements. Here is a MSDN link
In this event you can use FindControl method to find your control and set its Visible property false.

e.Row.FindControl("ControlID");

Upvotes: 2

Related Questions